1 % The UCW Macro Collection: Typesetting of algorithms
2 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
3 % -------------------------------------------------------------------------
7 % \algo{name of the algorithm}\algalias{a longer name}
8 % \algin input description
10 % \:step \cmt{comment}
11 % \::[identifier]nested step
12 % \:::yet more nested step
13 % \algout output description
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}
27 % Extra indent for nested items
28 \newdimen\algonestindent
32 \newdimen\algolabelwidth
35 % Set up spacing (customizable)
37 \itemnarrow=\itemindent
39 \preitemizeskip=\bigskipamount
40 \postitemizeskip=\dimexpr \bigskipamount - \parskip \relax
43 % Set up paragraph mode for a single step of the algorithm (customizable)
46 \parindent=\algoindent
47 \hangindent=\algoindent
48 \advance\hangindent by \algolabelwidth
53 % Typeset step label (customizable)
54 \def\algostep#1{\hbox to \algolabelwidth{\hss #1~~}}
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}
72 \def\endalgo{\endlist\endgroup}
76 \ifnum\itemcount=0\par\nobreak\smallskip\fi
77 \algopar\advance\itemcount by 1
78 \algostep{\the\itemcount.}%
79 \futurelet\next\algoitemh
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}
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)}
91 % Algorithm name in text or in a formula
92 \protected\def\alg#1{\leavevmode\hbox{\csc #1}}
95 \def\algcmt#1{\unskip~~\hfill\fullcmt{#1}\ignorespaces}
96 \def\fullcmt#1{{$\triangleleft$\hskip0.5em\it #1}}