Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 535 lines (534 sloc) 16.529 kB
08598ba Init.
Michael Aufreiter authored
1 % ALGORITHMIC STYLE -- Released 18 MAR 2004
2 % for LaTeX version 2e
3 %
4 % Copyright Szasz Janos
5 % E-mail szaszjanos@users.sourceforge.net
6 % Based on Peter Williams's algorithmic.sty
7 %
8 %
9 % *** INITIALISING ***
10 %
11 %
12 \NeedsTeXFormat{LaTeX2e}
13 \ProvidesPackage{algorithmicx}[2004/03/18 v1.1 Algorithmicx]
14 \RequirePackage{ifthen}
15 \typeout{Document Style algorithmicx 1.1 - a greatly improved `algorithmic' style}
16 %
17 \newcounter{ALG@line}
18 \newcounter{ALG@rem}
19 \newlength{\ALG@tlm}
20 \newlength{\ALG@ctlm}
21 \newlength{\algorithmicindent}
22 \setlength{\algorithmicindent}{1.5em}
23 \newcounter{ALG@blocknr}% the number of defined blocks
24 \setcounter{ALG@blocknr}{0}
25 \newcounter{ALG@tmpcounter}% only to decrement things
26 \def\algorithmicnoindent{-\ALG@tlm}
27 \def\algbackskipbegin{\hskip\ALG@ctlm}
28 \def\algbackskip{\hskip\ALG@ctlm\hskip-\ALG@tlm}
29 \def\algbackskipend{\hskip-\ALG@tlm}
30 \def\ALG@defaultindent{\algorithmicindent}
31 %
32 %
33 % *** ALGORITHMIC ***
34 %
35 %
36 \newenvironment{ALG@g}[1]% a list for the iner loops
37 {%
38 \begin{list}%
39 {\ALG@step}%
40 {%
41 \itemsep\z@%
42 \itemindent\z@%
43 \listparindent2em% the indent given to multiline lines...
44 \rightmargin\z@%
45 \topsep\z@%
46 \partopsep\z@%
47 \parskip\z@%
48 \parsep\z@%
49 \leftmargin#1%
50 \ALG@ctlm#1%
51 \addtolength{\ALG@tlm}{\leftmargin}%
52 \labelsep\ALG@tlm%
53 \addtolength{\labelsep}{0.5em}%
54 }%
55 \global\@newlistfalse% give no error, if the list is empty
56 }%
57 {\end{list}}%
58 %
59 % algorithmic environment
60 %
61 \let\ALG@beginalgorithmic\relax% for user overrides
62 \let\ALG@endalgorithmic\relax% for user overrides
63 \newenvironment{algorithmic}[1][0]%
64 {%
65 \def\@currentlabel{\theALG@line}%
66 \newcommand{\ALG@step}%
67 {%
68 \addtocounter{ALG@line}{1}%
69 \addtocounter{ALG@rem}{1}%
70 \ifthenelse{\equal{\arabic{ALG@rem}}{#1}}%
71 {\setcounter{ALG@rem}{0}{\footnotesize \arabic{ALG@line}:}}%
72 {}%
73 }%
74 %
75 \setcounter{ALG@line}{0}%
76 \setcounter{ALG@rem}{0}%
77 %
78 \let\@listii\@listi%
79 \let\@listiii\@listi%
80 \let\@listiv\@listi%
81 \let\@listv\@listi%
82 \let\@listvi\@listi%
83 \let\@listvii\@listi%
84 \renewcommand{\@toodeep}{}%
85 %
86 \renewcommand{\\}% for multiline parameters !!! needs fix
87 {%
88 \item%
89 \hskip\ALG@parindent%!!! not yet implemented
90 % \hskip-\algorithmicindent%
91 }%
92 %
93 \edef\ALG@currentitem{0}%
94 \let\ALG@currentvalue\relax%
95 %
96 \begin{list}%
97 {\ALG@step}%
98 {%
99 \rightmargin\z@%
100 \itemsep\z@ \itemindent\z@ \listparindent2em%
101 \partopsep\z@ \parskip\z@ \parsep\z@%
102 \labelsep 0.5em \topsep 0.0em%\skip 1.2em
103 \ifthenelse{\equal{#1}{0}}%
104 {\labelwidth 0.5em}%
105 {\labelwidth 1.2em}%
106 \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}% Ok. the perfect leftmargin :-))
107 \ALG@tlm 0em%
108 \ALG@ctlm\ALG@tlm%
109 }%
110 \ALG@beginalgorithmic%
111 }%
112 {% end{algorithmic}
113 % \global\@newlistfalse% protects against empty algorithms, not needed
114 % check if all blocks are closed
115 \ALG@closeloops%
116 \ifnum0=\ALG@currentitem\relax%
117 \else%
118 \PackageError{algorithmicx}{Some blocks are not closed!!!}{Yes, you have some open blocks.}%
119 \fi%
120 \ALG@endalgorithmic%
121 \end{list}%
122 }%
123 %
124 %
125 % *** Functional core ***
126 %
127 %
128 \def\ALG@makeentitie#1% execute the entitie (#1)
129 {%
130 \expandafter\ifx\csname ALG@b@#1@0\endcsname\relax%
131 \ALG@makenobegin{#1}% this entitie ends or continues blocks
132 \else%
133 \ALG@makebegin{#1}% this entitie can open blocks
134 \fi%
135 \ALG@entitiecommand%
136 }%
137 %
138 \def\ALG@makebegin#1% executes an entitie (#1) that can open blocks
139 {%
140 \ifx\ALG@currentvalue\relax%
141 \ALG@openblock{0}{#1}% in infinite block I can open my block
142 \else%
143 \ifnum0<\ALG@currentvalue\relax%
144 \ifnum\ALG@currentvalue>65534\else%
145 \setcounter{ALG@tmpcounter}{\ALG@currentvalue}% the block has 'space' for another included block
146 \addtocounter{ALG@tmpcounter}{-1}%
147 \edef\ALG@currentvalue{\arabic{ALG@tmpcounter}}%
148 \fi%
149 \ALG@openblock{0}{#1}%
150 \else% the block needs to be closed
151 \expandafter\ifx\csname ALG@b@#1@\ALG@currentitem\endcsname\relax%
152 \end{ALG@g}% I can not close this block, continue after it is closed by force
153 \ALG@makebegin{#1}%
154 \else%
155 \ALG@changeblock{#1}% close this block
156 \fi%
157 \fi%
158 \fi%
159 }%
160 %
161 \def\ALG@makenobegin#1% executes an entitie (#1) that can not open blocks
162 {%
163 \ifx\ALG@currentvalue\relax%
164 \ALG@changeblock{#1}% an infinite block must be broken
165 \else%
166 \expandafter\ifx\csname ALG@b@#1@\ALG@currentitem\endcsname\relax%
167 \end{ALG@g}% the block must be ended by force,
168 \ALG@makenobegin{#1}% the command still runs
169 \else%
170 \ALG@changeblock{#1}% i can continue / end this block, let's do it
171 \fi%
172 \fi%
173 }%
174 %
175 \def\ALG@ifnotrelax#1#2#3% #1 - the number of the closed block, #2 - the entitie, #3 - things to be done
176 {%
177 \edef\ALG@tmpcmd{\expandafter\noexpand\csname algorithmic#2\endcsname}%
178 \expandafter\ifx\csname ALG@t@#1@#2\endcsname\relax\else%
179 \expandafter\ifx\csname ALG@t@#1@#2\endcsname\ALG@tmpcmd%
180 \expandafter\ifx\csname algorithmic#2\endcsname\relax\else%
181 #3%
182 \fi%
183 \else%
184 #3%
185 \fi%
186 \fi%
187 }%
188 %
189 \def\ALG@closeblock#1#2% #1 - the number of the closed block, #2 - the entitie
190 {%
191 \ALG@ifnotrelax{#1}{#2}{\item}%
192 \expandafter\let\expandafter\ALG@entitiecommand\csname ALG@t@#1@#2\endcsname%
193 }%
194 %
195 \def\ALG@openblock#1#2% #1 - the number of the closed block, #2 - the entitie
196 {%
197 \item[]%
198 \begin{ALG@g}{\csname ALG@i@#2@#1\endcsname}%
199 \edef\ALG@currentitem{\csname ALG@b@#2@#1\endcsname}%
200 \expandafter\ifx\csname ALG@c@#2@#1\endcsname\relax%
201 \let\ALG@currentvalue\relax%
202 \else%
203 \edef\ALG@currentvalue{\csname ALG@c@#2@#1\endcsname}%
204 \fi%
205 \ALG@ifnotrelax{#1}{#2}%
206 {%
207 \item\hskip-\ALG@ctlm% default spacing
208 }%
209 \expandafter\let\expandafter\ALG@entitiecommand\csname ALG@t@#1@#2\endcsname%
210 }%
211 %
212 \def\ALG@changeblock#1% #1 - the entitie
213 {%
214 \setcounter{ALG@tmpcounter}{\ALG@currentitem}%
215 \expandafter\ifnum0=\csname ALG@b@#1@\ALG@currentitem\endcsname\relax%
216 \end{ALG@g}%
217 \ALG@closeblock{\arabic{ALG@tmpcounter}}{#1}%
218 \else%
219 \end{ALG@g}%
220 \ALG@openblock{\arabic{ALG@tmpcounter}}{#1}%
221 \fi%
222 }%
223 %
224 \def\ALG@closeloops% closes all finite blocks
225 {%
226 \ifx\ALG@currentvalue\relax%
227 \else% only if it is finite
228 \end{ALG@g}% the block must be ended by force,
229 \ALG@closeloops% the command still runs
230 \fi%
231 }%
232 %
233 %
234 % *** Low level block/entitie defining commands ***
235 %
236 %
237 \def\ALG@bl@{0}% the BIG block
238 \let\ALG@bl@@\ALG@bl@% the BIG block
239 %
240 % Create a block
241 %
242 \def\ALG@createblock#1% create the block #1, if it does not exists
243 {%
244 \@ifundefined{ALG@bl@#1}% needs to be created?
245 {%
246 \addtocounter{ALG@blocknr}{1}% increment the block counter
247 \expandafter\edef\csname ALG@bl@#1\endcsname{\arabic{ALG@blocknr}}% set the block number
248 }%
249 {}%
250 }%
251 %
252 % Get the block number
253 %
254 \def\ALG@getblocknumber#1{\csname ALG@bl@#1\endcsname}%
255 %
256 % Create an entitie
257 %
258 \def\ALG@createentitie#1% create the entitie #1, if it does not exists
259 {%
260 \@ifundefined{#1}% needs to be created?
261 {%
262 \expandafter\edef\csname #1\endcsname{\noexpand\ALG@makeentitie{#1}}%
263 \@ifundefined{algorithmic#1}% \algorithmic#1 exists?
264 {%
265 \expandafter\edef\csname algorithmic#1\endcsname{\noexpand\textbf{#1}}%
266 }%
267 {}%
268 }%
269 {}%
270 }%
271 %
272 \def\ALG@createtext#1#2% #1 = closed block; #2 = entitie; creates \ALG@t@#1@#2
273 {%
274 \expandafter\edef\csname ALG@t@#1@#2\endcsname%
275 {%
276 \expandafter\noexpand\csname algorithmic#2\endcsname%
277 }%
278 }%
279 %
280 % End and Continue block
281 %
282 \def\ALG@endandcontinueblock#1#2#3#4#5% #1 = new block; #2 = old block; #3 = entitie; #4 = credits; #5 = indent
283 {%
284 \ifthenelse{\equal{#3}{}}{}% execute only if the entity is not empty
285 {%
286 \ALG@createentitie{#3}% create the entitie
287 \ALG@createblock{#2}% create the old block, if needed
288 \ifthenelse{\equal{#1}{}}% whe need to open a new block?
289 {\expandafter\edef\csname ALG@b@#3@\ALG@getblocknumber{#2}\endcsname{0}}% no, just close the old one
290 {% yes,
291 \ALG@createblock{#1}% create the block
292 \expandafter\edef\csname ALG@b@#3@\ALG@getblocknumber{#2}\endcsname{\ALG@getblocknumber{#1}}% ending the old block opens a new one
293 \ifthenelse{\equal{#4}{}}% infinite or finite credits?
294 {\expandafter\let\csname ALG@c@#3@\ALG@getblocknumber{#2}\endcsname\relax}% infinite credits
295 {\expandafter\edef\csname ALG@c@#3@\ALG@getblocknumber{#2}\endcsname{#4}}% finite credits
296 \ifthenelse{\equal{#5}{}}% default or specified indentation
297 {\expandafter\let\csname ALG@i@#3@\ALG@getblocknumber{#2}\endcsname\ALG@defaultindent}% default indentation
298 {\expandafter\edef\csname ALG@i@#3@\ALG@getblocknumber{#2}\endcsname{#5}}% indentation is specified
299 }%
300 \ALG@createtext{\ALG@getblocknumber{#2}}{#3}%
301 }%
302 }%
303 %
304 %
305 % *** NEW VERSION ***
306 %
307 %
308 % used macros
309 %
310 \def\ALG@p@endtext@E{\algrenewtext{\ALG@v@end}}%
311 \def\ALG@p@endtext@xE{\algrenewtext[\ALG@v@newblock]{\ALG@v@end}}%
312 \def\ALG@p@endtext@N{\algnotext{\ALG@v@end}}%
313 \def\ALG@p@endtext@xN{\algnotext[\ALG@v@newblock]{\ALG@v@end}}%
314 \def\ALG@p@endtext@{}%
315 % starttext defines are more compex -- care must be taken for the optional parameters
316 \def\ALG@p@starttext@S{\ALG@p@s@process{\algrenewtext}}%
317 \def\ALG@p@starttext@C{\ALG@p@s@process{\algrenewtext}}%
318 \def\ALG@p@starttext@xC{\ALG@p@s@process{\algrenewtext[\ALG@v@oldblock]}}%
319 \def\ALG@p@s@process#1%
320 {%
321 \ifthenelse{\equal{\ALG@v@start}{}}%
322 {\ALG@p@endtext}%
323 {\@ifnextchar{[}{\ALG@p@s@getparamcount{#1}}{\ALG@p@s@simple{#1}}}%
324 }%
325 \def\ALG@p@s@getparamcount#1[#2]%
326 {%
327 \@ifnextchar{[}{\ALG@p@s@getdefparam{#1}{#2}}{\ALG@p@s@param{#1}{#2}}%
328 }%
329 \def\ALG@p@s@getdefparam#1#2[#3]%
330 {%
331 \ALG@p@s@defparam{#1}{#2}{#3}%
332 }%
333 \def\ALG@p@s@simple#1#2{#1{\ALG@v@start}{#2}\ALG@p@endtext}%
334 \def\ALG@p@s@param#1#2#3{#1{\ALG@v@start}[#2]{#3}\ALG@p@endtext}%
335 \def\ALG@p@s@defparam#1#2#3#4{#1{\ALG@v@start}[#2][#3]{#4}\ALG@p@endtext}%
336 % the rest of the crew
337 \def\ALG@p@starttext@{\ALG@p@endtext}%
338 \def\ALG@p@indent@def#1{\def\ALG@v@indent{#1}\ALG@p@setup}%
339 \def\ALG@p@indent@{\def\ALG@v@indent{}\ALG@p@setup}%
340 \def\ALG@p@credits@def#1{\def\ALG@v@credits{#1}\ALG@p@indent}%
341 \def\ALG@p@credits@{\ALG@p@indent}%
342 \def\ALG@p@end@def#1{\def\ALG@v@end{#1}\ALG@p@credits}%
343 \def\ALG@p@end@{\def\ALG@v@end{}\ALG@p@credits}%
344 \def\ALG@p@start@def#1{\def\ALG@v@start{#1}\ALG@p@end}%
345 \def\ALG@p@start@{\def\ALG@v@start{}\ALG@p@end}%
346 \def\ALG@p@oldblock@def#1{\def\ALG@v@oldblock{#1}\ALG@p@start}%
347 \def\ALG@p@oldblock@{\def\ALG@v@oldblock{}\ALG@p@start}%
348 \newcommand\ALG@p@newblock[1][]{\def\ALG@v@newblock{#1}\ALG@p@oldblock}%
349 \def\ALG@p@setup%
350 {%
351 \ifthenelse{\equal{\ALG@v@newblock}{}}%
352 {%
353 \ifthenelse{\equal{\ALG@v@start}{}}%
354 {%
355 \PackageError{algorithmicx}{Block or starting entitie must be specified!!!}{}%
356 }%
357 {%
358 \let\ALG@v@newblock\ALG@v@start%
359 }%
360 }%
361 {%
362 }%
363 \ALG@endandcontinueblock%
364 {\ALG@v@newblock}{\ALG@v@oldblock}{\ALG@v@start}%
365 {\ALG@v@credits}{\ALG@v@indent}%
366 \ALG@endandcontinueblock%
367 {}{\ALG@v@newblock}{\ALG@v@end}%
368 {}{}%
369 \ALG@p@starttext%
370 }%
371 %
372 % param handling
373 %
374 \newcommand\ALG@p@def[2][def]%
375 {%
376 \expandafter\let\csname ALG@p@#2\expandafter\endcsname\csname ALG@p@#2@#1\endcsname%
377 }%
378 \def\ALG@p@undef{\ALG@p@def[]}%
379 %
380 \def\ALG@p@ons{\ALG@p@def{start}}%
381 \def\ALG@p@onS{\ALG@p@def{start}\ALG@p@def[S]{starttext}}%
382 \def\ALG@p@onc{\ALG@p@def{oldblock}\ALG@p@def{start}}%
383 \def\ALG@p@onC{\ALG@p@def{oldblock}\ALG@p@def{start}\ALG@p@def[C]{starttext}}%
384 \def\ALG@p@onxC{\ALG@p@def{oldblock}\ALG@p@def{start}\ALG@p@def[xC]{starttext}}%
385 \def\ALG@p@one{\ALG@p@def{end}}%
386 \def\ALG@p@onE{\ALG@p@def{end}\ALG@p@def[E]{endtext}}%
387 \def\ALG@p@onxE{\ALG@p@def{end}\ALG@p@def[xE]{endtext}}%
388 \def\ALG@p@onN{\ALG@p@def{end}\ALG@p@def[N]{endtext}}%
389 \def\ALG@p@onxN{\ALG@p@def{end}\ALG@p@def[xN]{endtext}}%
390 \def\ALG@p@onb{\def\ALG@v@credits{}}%
391 \def\ALG@p@onl{\def\ALG@v@credits{1}}%
392 \def\ALG@p@onL{\ALG@p@def{credits}}%
393 \def\ALG@p@oni{\ALG@p@def{indent}}%
394 \expandafter\def\csname ALG@p@on]\endcsname{\let\ALG@p@rec\relax}%
395 %
396 % param processing
397 %
398 \def\ALG@p@process#1%
399 {%
400 \@ifundefined{ALG@p@on#1}%
401 {\typeout{algdef: Ignoring unknown token #1}}%
402 {\csname ALG@p@on#1\endcsname}%
403 \ALG@p@rec%
404 }%
405 %
406 \def\ALG@p@onx{\let\ALG@p@rec\ALG@p@processx}%
407 %
408 \def\ALG@p@processx#1%
409 {%
410 \@ifundefined{ALG@p@onx#1}%
411 {%
412 \typeout{algdef: Ignoring sole x}%
413 \def\ALG@p@rec{\let\ALG@p@rec\ALG@p@process\ALG@p@process#1}%
414 }%
415 {\csname ALG@p@onx#1\endcsname\let\ALG@p@rec\ALG@p@process}%
416 \ALG@p@rec%
417 }%
418 %
419 %
420 % *** User level block/entitie commands ***
421 %
422 %
423 %
424 % algdef{switches}... -- the king of all definitions in the algorithmicx package
425 %
426 \newcommand\algdef[1]%
427 {%
428 \ALG@p@undef{oldblock}%
429 \ALG@p@undef{start}%
430 \ALG@p@undef{end}%
431 \def\ALG@v@credits{}%
432 \ALG@p@undef{credits}%
433 \ALG@p@undef{indent}%
434 \ALG@p@undef{starttext}%
435 \ALG@p@undef{endtext}%
436 \let\ALG@p@rec\ALG@p@process%
437 \ALG@p@rec#1]%
438 \ALG@p@newblock%
439 }%
440 %
441 % a lot of other macros are provided for convenience
442 %
443 \def\algblock{\algdef{se}}%
444 \def\algcblock{\algdef{ce}}%
445 \def\algloop{\algdef{sl}}%
446 \def\algcloop{\algdef{cl}}%
447 \def\algsetblock{\algdef{seLi}}%
448 \def\algsetcblock{\algdef{ceLi}}%
449 \def\algblockx{\algdef{SxE}}%
450 \def\algblockdefx{\algdef{SE}}%
451 \def\algcblockx{\algdef{CxE}}%
452 \def\algcblockdefx{\algdef{CE}}%
453 \def\algsetblockx{\algdef{SxELi}}%
454 \def\algsetblockdefx{\algdef{SELi}}%
455 \def\algsetcblockx{\algdef{CxELi}}%
456 \def\algsetcblockdefx{\algdef{CELi}}%
457 \def\algloopdefx{\algdef{Sl}}%
458 \def\algcloopx{\algdef{xCl}}%
459 \def\algcloopdefx{\algdef{Cl}}%
460 % algloopx is not correct, use algloopdefx
461 %
462 % Text output commands
463 %
464 \newcommand\algrenewtext[2][]% [block]{entity}
465 {%
466 \ifthenelse{\equal{#2}{}}{}%
467 {%
468 \algnotext[#1]{#2}%
469 \ifthenelse{\equal{#1}{}}%
470 {%
471 \expandafter\newcommand\csname algorithmic#2\endcsname%
472 }%
473 {%
474 \expandafter\newcommand\csname ALG@t@\ALG@getblocknumber{#1}@#2\endcsname%
475 }%
476 }%
477 }%
478 %
479 \newcommand\algnotext[2][]% [block]{entity}
480 {%
481 \ifthenelse{\equal{#2}{}}{}%
482 {%
483 \ifthenelse{\equal{#1}{}}%
484 {%
485 \expandafter\let\csname algorithmic#2\endcsname\relax%
486 }%
487 {%
488 \expandafter\let\csname ALG@t@\ALG@getblocknumber{#1}@#2\endcsname\relax%
489 }%
490 }%
491 }%
492 %
493 \newcommand\algdefaulttext[2][]% [block]{entity}
494 {%
495 \ifthenelse{\equal{#1}{}}%
496 {%
497 \expandafter\def\csname algorithmic#2\endcsname{\textbf{#2}}%
498 }%
499 {%
500 \ALG@createtext{\ALG@getblocknumber{#1}}{#2}%
501 }%
502 }%
503 %
504 \def\ALG@notext*{\algnotext}%
505 \def\algtext{\@ifnextchar{*}{\ALG@notext}{\algrenewtext}}%
506 %
507 %
508 % *** OTHERS ***
509 %
510 %
511 \algdef{SL}[STATE]{State}{0}{}%
512 \def\BState{\State \algbackskip}%
513 \def\Statex{\item[]}% an empty line
514 \newcommand\algorithmiccomment[1]{\hfill\(\triangleright\) #1}%
515 \def\Comment{\algorithmiccomment}%
516 \def\algref#1#2{\ref{#1}.\ref{#2}}%
517 %
518 %
519 % *** LANGUAGE SWITCHING ***
520 %
521 %
522 \newcommand\ALG@deflanguage[3]%#1#2#3%
523 {%
524 \ifthenelse{\equal{#2}{}}%
525 {\expandafter\def\csname ALG@setlanguage@#1\endcsname{#3}}%
526 {\def\ALG@tmp{\let\ALG@tmp\relax#3}\ALG@tmp}%
527 }%
528 %
529 \def\algsetlanguage#1%
530 {%
531 \csname ALG@setlanguage@#1\endcsname%
532 }%
533 %
534 %
Something went wrong with that request. Please try again.