]> mj.ucw.cz Git - ucwmac.git/blob - ucw-algo.tex
961e319d000e03ccd4573cc2391961f60f688d70
[ucwmac.git] / ucw-algo.tex
1 % The UCW Macro Collection: Typesetting of algorithms
2 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
3 % -------------------------------------------------------------------------
4
5 %       \algo{name of the algorithm}\algalias{a longer name}
6 %       \algin input description
7 %       \:step
8 %       \:step
9 %       \::[identifier]nested step
10 %       \:::yet more nested step
11 %       \algout output description
12 %       \endalgo
13
14 \def\localealgo{Algorithm}
15 \def\localeproc{Procedure}
16 \def\localeinput{Input}
17 \def\localeoutput{Output}
18
19 \def\algoinit{
20 \goodbreak
21 \begingroup
22 \itemnarrow=\itemindent
23 \interitemskip=1pt
24 \preitemizeskip=\bigskipamount
25 \postitemizeskip=\dimexpr \bigskipamount - \parskip \relax
26 \inititemize
27 \let\:=\algoitem
28 \def\={\leftarrow}
29 \itemcount=0
30 }
31 \def\typedalgo#1#2{\algoinit {\bo #1\/} \alg{#2}}
32 \def\algo#1{\typedalgo{\localealgo}{#1}}
33 \def\proc#1{\typedalgo{\localeproc}{#1}}
34 \def\headlessalgo{\algoinit}
35 \def\endalgo{\goodbreak\endlist\endgroup}
36 \let\endproc=\endalgo
37 \def\algopar{\par
38 \parindent=2em
39 \hangindent=2.5em       % \parindent + width of box for the step number
40 \hangafter=1
41 \leavevmode
42 }
43 \def\algoitem{
44 \ifnum\itemcount=0\par\nobreak\smallskip\fi
45 \algopar\advance\itemcount by 1
46 \hbox to 0.5em{\hss \the\itemcount.~~}%
47 \futurelet\next\algoitemh}
48 \def\algoitemh{\ifx\next:\let\next=\algohang\else\let\next=\algoitemgo\fi\next}
49 \def\algohang:{\advance\hangindent by 2em \hskip 2em\futurelet\next\algoitemh}
50 \optdef\algoitemgo{\ifx\addid\undefined\else\addid{item}{\the\itemcount}\fi}
51 \def\algin{\par\nobreak{\it \localeinput:\/} }
52 \def\algout{\par\nobreak\parindent=0pt{\it localeoutput:\/} }
53 \def\alginbreak{\hfil\break\hphantom{\it \localeinput:\/} }
54 \def\algalias#1{ (#1)}
55
56 % Algorithm name in text or in a formula
57 \protected\def\alg#1{\leavevmode\hbox{\csc #1}}