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