]> mj.ucw.cz Git - ucwmac.git/commitdiff
A new module for typesetting algorithms
authorMartin Mares <mj@ucw.cz>
Thu, 31 May 2018 15:28:27 +0000 (17:28 +0200)
committerMartin Mares <mj@ucw.cz>
Thu, 31 May 2018 15:28:27 +0000 (17:28 +0200)
ucw-algo.tex [new file with mode: 0644]

diff --git a/ucw-algo.tex b/ucw-algo.tex
new file mode 100644 (file)
index 0000000..961e319
--- /dev/null
@@ -0,0 +1,57 @@
+% The UCW Macro Collection: Typesetting of algorithms
+% Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
+% -------------------------------------------------------------------------
+
+%      \algo{name of the algorithm}\algalias{a longer name}
+%      \algin input description
+%      \:step
+%      \:step
+%      \::[identifier]nested step
+%      \:::yet more nested step
+%      \algout output description
+%      \endalgo
+
+\def\localealgo{Algorithm}
+\def\localeproc{Procedure}
+\def\localeinput{Input}
+\def\localeoutput{Output}
+
+\def\algoinit{
+\goodbreak
+\begingroup
+\itemnarrow=\itemindent
+\interitemskip=1pt
+\preitemizeskip=\bigskipamount
+\postitemizeskip=\dimexpr \bigskipamount - \parskip \relax
+\inititemize
+\let\:=\algoitem
+\def\={\leftarrow}
+\itemcount=0
+}
+\def\typedalgo#1#2{\algoinit {\bo #1\/} \alg{#2}}
+\def\algo#1{\typedalgo{\localealgo}{#1}}
+\def\proc#1{\typedalgo{\localeproc}{#1}}
+\def\headlessalgo{\algoinit}
+\def\endalgo{\goodbreak\endlist\endgroup}
+\let\endproc=\endalgo
+\def\algopar{\par
+\parindent=2em
+\hangindent=2.5em      % \parindent + width of box for the step number
+\hangafter=1
+\leavevmode
+}
+\def\algoitem{
+\ifnum\itemcount=0\par\nobreak\smallskip\fi
+\algopar\advance\itemcount by 1
+\hbox to 0.5em{\hss \the\itemcount.~~}%
+\futurelet\next\algoitemh}
+\def\algoitemh{\ifx\next:\let\next=\algohang\else\let\next=\algoitemgo\fi\next}
+\def\algohang:{\advance\hangindent by 2em \hskip 2em\futurelet\next\algoitemh}
+\optdef\algoitemgo{\ifx\addid\undefined\else\addid{item}{\the\itemcount}\fi}
+\def\algin{\par\nobreak{\it \localeinput:\/} }
+\def\algout{\par\nobreak\parindent=0pt{\it localeoutput:\/} }
+\def\alginbreak{\hfil\break\hphantom{\it \localeinput:\/} }
+\def\algalias#1{ (#1)}
+
+% Algorithm name in text or in a formula
+\protected\def\alg#1{\leavevmode\hbox{\csc #1}}