]> mj.ucw.cz Git - ucwmac.git/blob - ucw-algo.tex
b773396d472f5efcbcb4533ead1b89d50171716d
[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 \def\algoinit{
23 \goodbreak
24 \begingroup
25 \itemnarrow=\itemindent
26 \interitemskip=1pt
27 \preitemizeskip=\bigskipamount
28 \postitemizeskip=\dimexpr \bigskipamount - \parskip \relax
29 \inititemize
30 \let\:=\algoitem
31 \def\={\leftarrow}
32 \itemcount=0
33 }
34 \def\typedalgo#1#2{\algoinit {\bo #1\/} \alg{#2}}
35 \def\algo#1{\typedalgo{\localestr{alg:alg}}{#1}}
36 \def\proc#1{\typedalgo{\localestr{alg:proc}}{#1}}
37 \def\headlessalgo{\algoinit}
38 \def\endalgo{\goodbreak\endlist\endgroup}
39 \let\endproc=\endalgo
40 \def\algopar{\par
41 \parindent=2em
42 \hangindent=2.5em       % \parindent + width of box for the step number
43 \hangafter=1
44 \leavevmode
45 }
46 \def\algoitem{
47 \ifnum\itemcount=0\par\nobreak\smallskip\fi
48 \algopar\advance\itemcount by 1
49 \hbox to 0.5em{\hss \the\itemcount.~~}%
50 \futurelet\next\algoitemh}
51 \def\algoitemh{\ifx\next:\let\next=\algohang\else\let\next=\algoitemgo\fi\next}
52 \def\algohang:{\advance\hangindent by 2em \hskip 2em\futurelet\next\algoitemh}
53 \optdef\algoitemgo{\ifx\addid\undefined\else\addid{item}{\the\itemcount}\fi}
54 \def\algin{\par\nobreak{\it \localestr{alg:in}:\/} }
55 \def\algout{\par\nobreak\parindent=0pt{\it \localestr{alg:in}:\/} }
56 \def\alginbreak{\hfil\break\hphantom{\it \localestr{alg:out}:\/} }
57 \def\algalias#1{ (#1)}
58
59 % Algorithm name in text or in a formula
60 \protected\def\alg#1{\leavevmode\hbox{\csc #1}}