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