]> mj.ucw.cz Git - ucwmac.git/blob - ucw-algo.tex
ucw-luaofs: Fixed a typo in comment
[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  \cmt{comment}
11 %       \::[identifier]nested step
12 %       \:::yet more nested step
13 %       \algout output description
14 %       \endalgo
15
16 % Localized strings
17 \localedef{0}{alg:alg}{Algorithm}
18 \localedef{0}{alg:proc}{Procedure}
19 \localedef{0}{alg:func}{Function}
20 \localedef{0}{alg:in}{Input}
21 \localedef{0}{alg:out}{Output}
22
23 % Item indent
24 \newdimen\algoindent
25 \algoindent=2em
26
27 % Extra indent for nested items
28 \newdimen\algonestindent
29 \algonestindent=2em
30
31 % Width of item label
32 \newdimen\algolabelwidth
33 \algolabelwidth=0.5em
34
35 % Set up spacing (customizable)
36 \def\algospacing{
37         \itemnarrow=\itemindent
38         \interitemskip=1pt
39         \preitemizeskip=\bigskipamount
40         \postitemizeskip=\dimexpr \bigskipamount - \parskip \relax
41 }
42
43 % Set up paragraph mode for a single step of the algorithm (customizable)
44 \def\algopar{
45         \par
46         \parindent=\algoindent
47         \hangindent=\algoindent
48         \advance\hangindent by \algolabelwidth
49         \hangafter=1
50         \leavevmode
51 }
52
53 % Typeset step label (customizable)
54 \def\algostep#1{\hbox to \algolabelwidth{\hss #1~~}}
55
56 \def\algoinit{
57         \begingroup
58         \algospacing
59         \inititemize
60         \let\:=\algoitem
61         \def\={\leftarrow}
62         \let\cmt=\algcmt
63         \itemcount=0
64 }
65
66 \def\typedalgo#1#2{\algoinit {\bo #1\/} \alg{#2}}
67 \def\algo#1{\typedalgo{\localestr{alg:alg}}{#1}}
68 \def\proc#1{\typedalgo{\localestr{alg:proc}}{#1}}
69 \def\func#1{\typedalgo{\localestr{alg:func}}{#1}}
70 \def\headlessalgo{\algoinit}
71
72 \def\endalgo{\endlist\endgroup}
73 \let\endproc=\endalgo
74
75 \def\algoitem{%
76         \ifnum\itemcount=0\par\nobreak\smallskip\fi
77         \algopar\advance\itemcount by 1
78         \algostep{\the\itemcount.}%
79         \futurelet\next\algoitemh
80 }
81
82 \def\algoitemh{\ifx\next:\let\next=\algohang\else\let\next=\algoitemgo\fi\next}
83 \def\algohang:{\advance\hangindent by \algonestindent \hskip \algonestindent\futurelet\next\algoitemh}
84 \optdef\algoitemgo{\ifx\addid\undefined\else\addid{item}{\the\itemcount}\fi}
85
86 \def\algin{\par\nobreak{\it \localestr{alg:in}:\/} }
87 \def\algout{\par\nobreak\parindent=0pt{\it \localestr{alg:out}:\/} }
88 \def\alginbreak{\hfil\break\hphantom{\it \localestr{alg:in}:\/} }
89 \def\algalias#1{ (#1)}
90
91 % Algorithm name in text or in a formula
92 \protected\def\alg#1{\leavevmode\hbox{\csc #1}}
93
94 % Comments
95 \def\algcmt#1{\unskip~~\hfill\fullcmt{#1}\ignorespaces}
96 \def\fullcmt#1{{$\triangleleft$\hskip0.5em\it #1}}