]> mj.ucw.cz Git - ucwmac.git/blob - ucw-algo.tex
ucw-algo: \algospacing and cleanup
[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 \ucwdefmodule{algo}
6
7 %       \algo{name of the algorithm}\algalias{a longer name}
8 %       \algin input description
9 %       \:step
10 %       \:step
11 %       \::[identifier]nested step
12 %       \:::yet more nested step
13 %       \algout output description
14 %       \endalgo
15
16 % Localized strings
17 \localedef{en}{alg:alg}{Algorithm}
18 \localedef{en}{alg:proc}{Procedure}
19 \localedef{en}{alg:in}{Input}
20 \localedef{en}{alg:out}{Output}
21
22 % Spacing (customizable)
23 \def\algospacing{
24         \itemnarrow=\itemindent
25         \interitemskip=1pt
26         \preitemizeskip=\bigskipamount
27         \postitemizeskip=\dimexpr \bigskipamount - \parskip \relax
28 }
29
30 \def\algoinit{
31         \goodbreak
32         \begingroup
33         \algospacing
34         \inititemize
35         \let\:=\algoitem
36         \def\={\leftarrow}
37         \itemcount=0
38 }
39
40 \def\typedalgo#1#2{\algoinit {\bo #1\/} \alg{#2}}
41 \def\algo#1{\typedalgo{\localestr{alg:alg}}{#1}}
42 \def\proc#1{\typedalgo{\localestr{alg:proc}}{#1}}
43 \def\headlessalgo{\algoinit}
44
45 \def\endalgo{\goodbreak\endlist\endgroup}
46 \let\endproc=\endalgo
47
48 \def\algopar{
49         \par
50         \parindent=2em
51         \hangindent=2.5em       % \parindent + width of box for the step number
52         \hangafter=1
53         \leavevmode
54 }
55
56 \def\algoitem{%
57         \ifnum\itemcount=0\par\nobreak\smallskip\fi
58         \algopar\advance\itemcount by 1
59         \hbox to 0.5em{\hss \the\itemcount.~~}%
60         \futurelet\next\algoitemh
61 }
62
63 \def\algoitemh{\ifx\next:\let\next=\algohang\else\let\next=\algoitemgo\fi\next}
64 \def\algohang:{\advance\hangindent by 2em \hskip 2em\futurelet\next\algoitemh}
65 \optdef\algoitemgo{\ifx\addid\undefined\else\addid{item}{\the\itemcount}\fi}
66
67 \def\algin{\par\nobreak{\it \localestr{alg:in}:\/} }
68 \def\algout{\par\nobreak\parindent=0pt{\it \localestr{alg:in}:\/} }
69 \def\alginbreak{\hfil\break\hphantom{\it \localestr{alg:out}:\/} }
70 \def\algalias#1{ (#1)}
71
72 % Algorithm name in text or in a formula
73 \protected\def\alg#1{\leavevmode\hbox{\csc #1}}