1 % ALGORITHMIC STYLE -- Released 27 APR 2005
4 % Copyright Szasz Janos
5 % E-mail szaszjanos@users.sourceforge.net
11 \NeedsTeXFormat{LaTeX2e}
12 \ProvidesPackage{algorithmicx}[2005/04/27 v1.2 Algorithmicx]
13 \RequirePackage{ifthen}
14 \typeout{Document Style algorithmicx 1.2 - a greatly improved `algorithmic' style}
18 \newcounter{ALG@nested}
20 \newlength{\ALG@thistlm}
21 \newcounter{ALG@Lnr}% the number of defined languages
22 \setcounter{ALG@Lnr}{0}
23 \newcounter{ALG@blocknr}% the number of defined blocks
24 \setcounter{ALG@blocknr}{0}
25 \newcounter{ALG@storecount}% number of stored but not restored algorithmic environments
26 \setcounter{ALG@storecount}{0}
27 \newcounter{ALG@tmpcounter}% only to decrement things
28 \newlength\ALG@tmplength%
29 %\def\algorithmicnoindent{-\ALG@tlm}
30 % \def\algbackskipbegin{\hskip\ALG@ctlm}
31 %\def\algbackskip{\hskip-\ALG@thistlm}
32 %\def\algbackskipend{\hskip-\ALG@tlm}
33 \def\ALG@defaultindent{\algorithmicindent}
37 \def\ALG@newcondstate#1%
39 \expandafter\edef\csname ALG@x@#1\endcsname%
40 {\expandafter\noexpand\csname @@ALG@x@#1\endcsname}%
42 \ALG@newcondstate{notext}%
43 \ALG@newcondstate{default}%
49 \newcommand\ALG@beginblock[1]% #1 - indentation
52 \addtolength\ALG@tlm{#1}%
53 \addtocounter{ALG@nested}{1}%
54 \setlength\ALG@tmplength{#1}%
55 \expandafter\edef\csname ALG@ind@\theALG@nested\endcsname{\the\ALG@tmplength}%
57 \newcommand\ALG@endblock%
59 \addtolength\ALG@tlm{-\csname ALG@ind@\theALG@nested\endcsname}%
60 \addtocounter{ALG@nested}{-1}%
64 % algorithmic environment
68 \addtocounter{ALG@line}{1}%
69 \addtocounter{ALG@rem}{1}%
70 \ifthenelse{\equal{\arabic{ALG@rem}}{\ALG@numberfreq}}%
71 {\setcounter{ALG@rem}{0}\alglinenumber{\arabic{ALG@line}}}%
74 \newenvironment{algorithmic}[1][0]%
76 \edef\ALG@numberfreq{#1}%
77 \def\@currentlabel{\theALG@line}%
79 \setcounter{ALG@line}{0}%
80 \setcounter{ALG@rem}{0}%
84 \expandafter\edef\csname ALG@currentblock@\theALG@nested\endcsname{0}%
85 \expandafter\let\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
91 \itemsep\z@ \itemindent\z@ \listparindent2em%
92 \partopsep\z@ \parskip\z@ \parsep\z@%
93 \labelsep 0.5em \topsep 0.2em%\skip 1.2em
94 \ifthenelse{\equal{#1}{0}}%
97 \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}% Ok. the perfect leftmargin :-))
100 \setcounter{ALG@nested}{0}%
101 \ALG@beginalgorithmic%
104 % check if all blocks are closed
106 \expandafter\ifnum\csname ALG@currentblock@\theALG@nested\endcsname=0\relax%
108 \PackageError{algorithmicx}{Some blocks are not closed!!!}{}%
115 % *** Functional core ***
118 \def\ALG@makeentity#1% execute the entity (#1)
120 \def\ALG@thisentity{#1}%
121 \expandafter\ifx\csname ALG@b@\ALG@L @#1@0\endcsname\relax%
122 \let\ALG@makenobeginrepeat\ALG@makenobegin\ALG@makenobeginrepeat% this entitie ends or continues blocks
124 \let\ALG@makebeginrepeat\ALG@makebegin\ALG@makebeginrepeat% this entitie can open blocks
129 \def\ALG@makebegin% executes an entitie that can open blocks
131 \expandafter\let\expandafter\ALG@thislifetime\csname ALG@currentlifetime@\theALG@nested\endcsname%
132 \ifx\ALG@thislifetime\relax%
133 \let\ALG@makebeginrepeat\ALG@doentity% in infinite block I can open my block
135 \ifnum\ALG@thislifetime>0\relax%
136 \ifnum\ALG@thislifetime>65534\else%
137 \setcounter{ALG@tmpcounter}{\ALG@thislifetime}% the block has 'space' for another included block
138 \addtocounter{ALG@tmpcounter}{-1}%
139 \expandafter\edef\csname ALG@currentlifetime@\theALG@nested\endcsname{\arabic{ALG@tmpcounter}}%
141 \let\ALG@makebeginrepeat\ALG@doentity%
142 \else% the block needs to be closed
143 \expandafter\ifx\csname ALG@b@\ALG@L @\ALG@thisentity @\csname ALG@currentblock@\theALG@nested\endcsname\endcsname\relax%
144 \ALG@closebyforce% I can not close this block, continue after it is closed by force
147 % the block would be closed automatically, but this entitie can close it, so let's do it with the entity
148 \let\ALG@makebeginrepeat\ALG@doentity%
152 \ALG@makebeginrepeat%
155 \def\ALG@makenobegin% executes an entitie that can not open blocks
157 \expandafter\ifx\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
158 \let\ALG@makenobeginrepeat\ALG@doentity% an infinite block must be broken
160 \expandafter\ifx\csname ALG@b@\ALG@L @\ALG@thisentity @\csname ALG@currentblock@\theALG@nested\endcsname\endcsname\relax%
161 \ALG@closebyforce% the block must be ended by force,
163 \let\ALG@makenobeginrepeat\ALG@doentity% I can continue / end this block, let's do it
166 \ALG@makenobeginrepeat%
171 \ALG@beginblock{\csname ALG@i@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname}%
172 \expandafter\edef\csname ALG@currentblock@\theALG@nested\endcsname{\csname ALG@b@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname}%
173 \expandafter\ifx\csname ALG@c@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname\relax%
174 \expandafter\let\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
176 \expandafter\edef\csname ALG@currentlifetime@\theALG@nested\endcsname{\csname ALG@c@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname}%
185 \def\ALG@doentity% the number of the closed block, the entitie
187 \edef\ALG@thisblock{\csname ALG@currentblock@\theALG@nested\endcsname}%
188 \expandafter\ifx\csname ALG@b@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname\relax%
189 \def\ALG@thisblock{0}%
192 \ifnum\ALG@thisblock=0\else\ALG@doend\fi%
193 \ifx\ALG@text\ALG@x@notext%
194 \item[]\nointerlineskip%\vskip-\prevdepth\nointerlineskip% bug: if there are no text and no lines, then this is wrong
198 \noindent\hskip\ALG@tlm%
199 \expandafter\ifnum0=\csname ALG@b@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname\else%
202 \def\ALG@entitiecommand{\ALG@displayentity}%
205 \def\ALG@getentitytext%
207 \expandafter\let\expandafter\ALG@text\csname ALG@t@\ALG@L @\ALG@thisentity @\ALG@thisblock\endcsname%
208 \ifx\ALG@text\ALG@x@default%
209 % block specific - default
210 \expandafter\let\expandafter\ALG@text\csname ALG@t@\ALG@L @\ALG@thisentity\endcsname%
211 \ifx\ALG@text\ALG@x@default%
212 % block specific - default, language specific - default
213 \def\ALG@text{\ALG@deftext{\ALG@thisentity}}%
218 \def\ALG@deftext{\csname ALG@deftext@\ALG@L\endcsname}%
220 \def\ALG@displayentity%
222 \ifx\ALG@text\ALG@x@notext%
228 \def\ALG@closebyforce%
233 \def\ALG@closeloops% closes all finite blocks
235 \expandafter\ifx\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
236 \else% only if it is finite
237 \ALG@closebyforce% the block must be ended by force,
238 \ALG@closeloops% the command still runs
243 % *** Low level block/entitie defining commands ***
246 \def\ALG@bl@{0}% the BIG block
247 \let\ALG@bl@@\ALG@bl@% the BIG block
251 \def\ALG@createblock#1% create the block #1, if it does not exists
253 \@ifundefined{ALG@bl@\ALG@Ld @#1}% needs to be created?
255 \addtocounter{ALG@blocknr}{1}% increment the block counter
256 \expandafter\edef\csname ALG@bl@\ALG@Ld @#1\endcsname{\arabic{ALG@blocknr}}% set the block number
261 % Get the block number
263 \def\ALG@getblocknumber#1{\csname ALG@bl@\ALG@Ld @#1\endcsname}%
267 \def\ALG@createentitie#1% create the entitie #1, if it does not exists
269 \expandafter\ALG@edefcmd\csname #1\endcsname{\noexpand\ALG@makeentity{#1}}%
270 \@ifundefined{ALG@t@\ALG@Ld @#1}% the entity text is defined in this language?
272 \expandafter\let\csname ALG@t@\ALG@Ld @#1\endcsname\ALG@x@default%
277 \def\ALG@createtext#1#2% #1 = closed block; #2 = entitie; creates \ALG@t@#2@#1
279 \expandafter\let\csname ALG@t@\ALG@Ld @#2@#1\endcsname\ALG@x@default%
282 % End and Continue block
284 \def\ALG@endandcontinueblock#1#2#3#4#5% #1 = new block; #2 = old block; #3 = entitie; #4 = credits; #5 = indent
286 \ifthenelse{\equal{#3}{}}{}% execute only if the entity is not empty
288 \ALG@createentitie{#3}% create the entitie
289 \ALG@createblock{#2}% create the old block, if needed
290 \ifthenelse{\equal{#1}{}}% whe need to open a new block?
291 {\expandafter\edef\csname ALG@b@\ALG@Ld @#3@\ALG@getblocknumber{#2}\endcsname{0}}% no, just close the old one
293 \ALG@createblock{#1}% create the block
294 \expandafter\edef\csname ALG@b@\ALG@Ld @#3@\ALG@getblocknumber{#2}\endcsname{\ALG@getblocknumber{#1}}% ending the old block opens a new one
295 \ifthenelse{\equal{#4}{}}% infinite or finite credits?
296 {\expandafter\let\csname ALG@c@\ALG@Ld @#3@\ALG@getblocknumber{#2}\endcsname\relax}% infinite credits
297 {\expandafter\edef\csname ALG@c@\ALG@Ld @#3@\ALG@getblocknumber{#2}\endcsname{#4}}% finite credits
298 \ifthenelse{\equal{#5}{}}% default or specified indentation
299 {\expandafter\let\csname ALG@i@\ALG@Ld @#3@\ALG@getblocknumber{#2}\endcsname\ALG@defaultindent}% default indentation
300 {\expandafter\edef\csname ALG@i@\ALG@Ld @#3@\ALG@getblocknumber{#2}\endcsname{#5}}% indentation is specified
302 \ALG@createtext{\ALG@getblocknumber{#2}}{#3}%
306 % macros used in declarations
308 \def\ALG@p@endtext@E{\algrenewtext{\ALG@v@end}}%
309 \def\ALG@p@endtext@xE{\algrenewtext[\ALG@v@newblock]{\ALG@v@end}}%
310 \def\ALG@p@endtext@nE{\algnotext{\ALG@v@end}}%
311 \def\ALG@p@endtext@xnE{\algnotext[\ALG@v@newblock]{\ALG@v@end}}%
312 \def\ALG@p@endtext@{}%
313 % starttext defines are more compex -- care must be taken for the optional parameters
314 \def\ALG@p@starttext@S{\ALG@p@s@process{\algrenewtext}}%
315 \def\ALG@p@starttext@C{\ALG@p@s@process{\algrenewtext}}%
316 \def\ALG@p@starttext@xC{\ALG@p@s@process{\algrenewtext[\ALG@v@oldblock]}}%
317 \def\ALG@p@s@process#1%
319 \ifthenelse{\equal{\ALG@v@start}{}}%
321 {\@ifnextchar{[}{\ALG@p@s@getparamcount{#1}}{\ALG@p@s@simple{#1}}}%
323 \def\ALG@p@s@getparamcount#1[#2]%
325 \@ifnextchar{[}{\ALG@p@s@getdefparam{#1}{#2}}{\ALG@p@s@param{#1}{#2}}%
327 \def\ALG@p@s@getdefparam#1#2[#3]%
329 \ALG@p@s@defparam{#1}{#2}{#3}%
331 \def\ALG@p@s@simple#1#2{#1{\ALG@v@start}{#2}\ALG@p@endtext}%
332 \def\ALG@p@s@param#1#2#3{#1{\ALG@v@start}[#2]{#3}\ALG@p@endtext}%
333 \def\ALG@p@s@defparam#1#2#3#4{#1{\ALG@v@start}[#2][#3]{#4}\ALG@p@endtext}%
334 % the rest of the crew
335 \def\ALG@p@starttext@nS{\algnotext{\ALG@v@start}\ALG@p@endtext}%
336 \def\ALG@p@starttext@nC{\algnotext{\ALG@v@start}\ALG@p@endtext}%
337 \def\ALG@p@starttext@xnC{\algnotext[\ALG@v@oldblock]{\ALG@v@start}\ALG@p@endtext}%
338 \def\ALG@p@starttext@{\ALG@p@endtext}%
339 \def\ALG@p@indent@def#1{\def\ALG@v@indent{#1}\ALG@p@setup}%
340 \def\ALG@p@indent@{\def\ALG@v@indent{}\ALG@p@setup}%
341 \def\ALG@p@credits@def#1{\def\ALG@v@credits{#1}\ALG@p@indent}%
342 \def\ALG@p@credits@{\ALG@p@indent}%
343 \def\ALG@p@end@def#1{\def\ALG@v@end{#1}\ALG@p@credits}%
344 \def\ALG@p@end@{\def\ALG@v@end{}\ALG@p@credits}%
345 \def\ALG@p@start@def#1{\def\ALG@v@start{#1}\ALG@p@end}%
346 \def\ALG@p@start@{\def\ALG@v@start{}\ALG@p@end}%
347 \def\ALG@p@oldblock@def#1{\def\ALG@v@oldblock{#1}\ALG@p@start}%
348 \def\ALG@p@oldblock@{\def\ALG@v@oldblock{}\ALG@p@start}%
349 \newcommand\ALG@p@newblock[1][]{\def\ALG@v@newblock{#1}\ALG@p@oldblock}%
352 \ifthenelse{\equal{\ALG@v@newblock}{}}%
354 \ifthenelse{\equal{\ALG@v@start}{}}%
356 \PackageError{algorithmicx}{Block or starting entitie must be specified!!!}{}%
359 \let\ALG@v@newblock\ALG@v@start%
364 \ALG@endandcontinueblock%
365 {\ALG@v@newblock}{\ALG@v@oldblock}{\ALG@v@start}%
366 {\ALG@v@credits}{\ALG@v@indent}%
367 \ALG@endandcontinueblock%
368 {}{\ALG@v@newblock}{\ALG@v@end}%
375 \newcommand\ALG@p@def[2][def]%
377 \expandafter\let\csname ALG@p@#2\expandafter\endcsname\csname ALG@p@#2@#1\endcsname%
379 \def\ALG@p@undef{\ALG@p@def[]}%
381 \def\ALG@p@ons{\ALG@p@def{start}}%
382 \def\ALG@p@onS{\ALG@p@def{start}\ALG@p@def[S]{starttext}}%
383 \def\ALG@p@onc{\ALG@p@def{oldblock}\ALG@p@def{start}}%
384 \def\ALG@p@onC{\ALG@p@def{oldblock}\ALG@p@def{start}\ALG@p@def[C]{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@onxC{\ALG@p@def{oldblock}\ALG@p@def{start}\ALG@p@def[xC]{starttext}}%
388 \def\ALG@p@onxE{\ALG@p@def{end}\ALG@p@def[xE]{endtext}}%
389 \def\ALG@p@onnS{\ALG@p@def{start}\ALG@p@def[nS]{starttext}}%
390 \def\ALG@p@onnC{\ALG@p@def{oldblock}\ALG@p@def{start}\ALG@p@def[nC]{starttext}}%
391 \def\ALG@p@onnE{\ALG@p@def{end}\ALG@p@def[nE]{endtext}}%
392 \def\ALG@p@onxnC{\ALG@p@def{oldblock}\ALG@p@def{start}\ALG@p@def[xnC]{starttext}}%
393 \def\ALG@p@onxnE{\ALG@p@def{end}\ALG@p@def[xnE]{endtext}}%
394 \def\ALG@p@onb{\def\ALG@v@credits{}}%
395 \def\ALG@p@onl{\def\ALG@v@credits{1}}%
396 \def\ALG@p@onL{\ALG@p@def{credits}}%
397 \def\ALG@p@oni{\ALG@p@def{indent}}%
401 \@ifundefined{ALG@ps@\ALG@p@state @#1}%
403 \csname ALG@ps@\ALG@p@state @other\endcsname{#1}%
406 \csname ALG@ps@\ALG@p@state @#1\endcsname%
410 % STATE : <<starting state>>
411 \expandafter\def\csname ALG@ps@@]\endcsname{\let\ALG@p@rec\relax}%
412 \def\ALG@ps@@s{\ALG@p@ons}%
413 \def\ALG@ps@@S{\ALG@p@onS}%
414 \def\ALG@ps@@c{\ALG@p@onc}%
415 \def\ALG@ps@@C{\ALG@p@onC}%
416 \def\ALG@ps@@e{\ALG@p@one}%
417 \def\ALG@ps@@E{\ALG@p@onE}%
418 \def\ALG@ps@@N{\typeout{algdef: 'N' obsoloted, use 'nE'.}\ALG@p@onnE}%
419 \def\ALG@ps@@b{\ALG@p@onb}%
420 \def\ALG@ps@@l{\ALG@p@onl}%
421 \def\ALG@ps@@L{\ALG@p@onL}%
422 \def\ALG@ps@@i{\ALG@p@oni}%
423 \def\ALG@ps@@x{\def\ALG@p@state{x}}%
424 \def\ALG@ps@@n{\def\ALG@p@state{n}}%
425 \def\ALG@ps@@other#1{\typeout{algdef: Ignoring unknown token #1}}%
427 \def\ALG@ps@x@C{\def\ALG@p@state{}\ALG@p@onxC}%
428 \def\ALG@ps@x@E{\def\ALG@p@state{}\ALG@p@onxE}%
429 \def\ALG@ps@x@N{\def\ALG@p@state{}\typeout{algdef: 'xN' obsoloted, use 'xnE'.}\ALG@p@onxnE}%
430 \def\ALG@ps@x@n{\def\ALG@p@state{xn}}%
431 \def\ALG@ps@x@other#1%
433 \typeout{algdef: Ignoring 'x' before '#1'.}%
435 \def\ALG@p@rec{\let\ALG@p@rec\ALG@p@main\ALG@p@rec#1}%
438 \def\ALG@ps@n@S{\def\ALG@p@state{}\ALG@p@onnS}%
439 \def\ALG@ps@n@C{\def\ALG@p@state{}\ALG@p@onnC}%
440 \def\ALG@ps@n@E{\def\ALG@p@state{}\ALG@p@onnE}%
441 \def\ALG@ps@n@x{\def\ALG@p@state{nx}}%
442 \def\ALG@ps@n@other#1%
444 \typeout{algdef: Ignoring 'n' before '#1'.}%
446 \def\ALG@p@rec{\let\ALG@p@rec\ALG@p@main\ALG@p@rec#1}%
449 \def\ALG@ps@xn@C{\def\ALG@p@state{}\ALG@p@onxnC}%
450 \def\ALG@ps@xn@E{\def\ALG@p@state{}\ALG@p@onxnE}%
451 \def\ALG@ps@xn@x{\typeout{algdef: Ignoring 'x' after 'xn'.}}%
452 \def\ALG@ps@xn@n{\typeout{algdef: Ignoring 'n' after 'xn'.}}%
453 \def\ALG@ps@xn@other#1%
455 \typeout{algdef: Ignoring 'xn' before '#1'.}%
457 \def\ALG@p@rec{\let\ALG@p@rec\ALG@p@main\ALG@p@rec#1}%
460 \def\ALG@ps@nx@C{\def\ALG@p@state{}\ALG@p@onxnC}%
461 \def\ALG@ps@nx@E{\def\ALG@p@state{}\ALG@p@onxnE}%
462 \def\ALG@ps@nx@x{\typeout{algdef: Ignoring 'x' after 'nx'.}}%
463 \def\ALG@ps@nx@n{\typeout{algdef: Ignoring 'n' after 'nx'.}}%
464 \def\ALG@ps@nx@other#1%
466 \typeout{algdef: Ignoring 'nx' before '#1'.}%
468 \def\ALG@p@rec{\let\ALG@p@rec\ALG@p@main\ALG@p@rec#1}%
472 % *** User level block/entitie commands ***
476 % algdef{switches}... -- the king of all definitions in the algorithmicx package
478 \newcommand\algdef[1]%
480 \ALG@p@undef{oldblock}%
483 \def\ALG@v@credits{}%
484 \ALG@p@undef{credits}%
485 \ALG@p@undef{indent}%
486 \ALG@p@undef{starttext}%
487 \ALG@p@undef{endtext}%
489 \let\ALG@p@rec\ALG@p@main%
494 % a lot of other macros are provided for convenience
496 \def\algblock{\algdef{se}}%
497 \def\algcblock{\algdef{ce}}%
498 \def\algloop{\algdef{sl}}%
499 \def\algcloop{\algdef{cl}}%
500 \def\algsetblock{\algdef{seLi}}%
501 \def\algsetcblock{\algdef{ceLi}}%
502 \def\algblockx{\algdef{SxE}}%
503 \def\algblockdefx{\algdef{SE}}%
504 \def\algcblockx{\algdef{CxE}}%
505 \def\algcblockdefx{\algdef{CE}}%
506 \def\algsetblockx{\algdef{SxELi}}%
507 \def\algsetblockdefx{\algdef{SELi}}%
508 \def\algsetcblockx{\algdef{CxELi}}%
509 \def\algsetcblockdefx{\algdef{CELi}}%
510 \def\algloopdefx{\algdef{Sl}}%
511 \def\algcloopx{\algdef{xCl}}%
512 \def\algcloopdefx{\algdef{Cl}}%
513 % algloopx is not correct, use algloopdefx
515 % Text output commands
517 \newcommand\algrenewtext[2][]% [block]{entity}
519 \ifthenelse{\equal{#2}{}}{}%
521 \ifthenelse{\equal{#1}{}}%
523 \expandafter\let\csname ALG@t@\ALG@Ld @#2\endcsname\relax%
524 \expandafter\newcommand\csname ALG@t@\ALG@Ld @#2\endcsname%
527 \expandafter\let\csname ALG@t@\ALG@Ld @#2@\ALG@getblocknumber{#1}\endcsname\relax%
528 \expandafter\newcommand\csname ALG@t@\ALG@Ld @#2@\ALG@getblocknumber{#1}\endcsname%
533 \def\ALG@letentitytext#1#2% [block]{entity}
535 \ifthenelse{\equal{#2}{}}{}%
537 \ifthenelse{\equal{#1}{}}%
539 \expandafter\let\csname ALG@t@\ALG@Ld @#2\endcsname%
542 \expandafter\let\csname ALG@t@\ALG@Ld @#2@\ALG@getblocknumber{#1}\endcsname%
547 \newcommand\algnotext[2][]% [block]{entity}
549 \ALG@letentitytext{#1}{#2}\ALG@x@notext%
552 \newcommand\algdefaulttext[2][]% [block]{entity}
554 \ALG@letentitytext{#1}{#2}\ALG@x@default%
557 \def\ALG@notext*{\algnotext}%
558 \def\algtext{\@ifnextchar{*}{\ALG@notext}{\algrenewtext}}%
561 % *** LANGUAGE SWITCHING ***
565 \newcommand\algnewlanguage[1]%
567 \@ifundefined{ALG@L@#1}% needs to be created?
570 \PackageError{algorithmicx}{Language '#1' already defined!}{}%
572 \addtocounter{ALG@Lnr}{1}% increment the language counter
573 \expandafter\edef\csname ALG@L@#1\endcsname{\arabic{ALG@Lnr}}% set the language number
574 \edef\ALG@Ld{\csname ALG@L@#1\endcsname}%
575 \expandafter\let\csname ALG@bl@\ALG@Ld @\endcsname\ALG@bl@% the BIG block
576 \expandafter\let\csname ALG@bl@\ALG@Ld @@\endcsname\ALG@bl@% the BIG block
577 \algdef{SL}[STATE]{State}{0}{}%
578 \expandafter\def\csname ALG@deftext@\ALG@Ld\endcsname{\textbf}%
579 \algnewcommand\algorithmiccomment[1]{\hfill\(\triangleright\) ##1}%
580 \algnewcommand\algorithmicindent{1.5em}%
581 \algnewcommand\alglinenumber[1]{\footnotesize ##1:}%
582 \algnewcommand\ALG@beginalgorithmic\relax% for user overrides
583 \algnewcommand\ALG@endalgorithmic\relax% for user overrides
586 \newcommand\algsetlanguage[1]%
588 \@ifundefined{ALG@L@#1}% needs to be created?
590 \PackageError{algorithmicx}{Language '#1' is not yet defined!}{}%
592 \edef\ALG@L{\csname ALG@L@#1\endcsname}%
595 \newcommand\algdeflanguage[1]%
597 \@ifundefined{ALG@L@#1}% needs to be created?
599 \PackageError{algorithmicx}{Language '#1' is not yet defined!}{}%
601 \edef\ALG@Ld{\csname ALG@L@#1\endcsname}%
604 \newcommand\alglanguage[1]%
611 % *** Defining language dependent stuff ***
614 \def\ALG@eatoneparam#1{}%
615 \def\ALG@defbasecmd#1#2%
617 \edef\ALG@tmp{\expandafter\ALG@eatoneparam\string #2}%
618 \@ifundefined\ALG@tmp{\edef #2{\noexpand\csname ALG@cmd@\noexpand\ALG@L @\ALG@tmp\endcsname}}{}%
619 \expandafter#1\csname ALG@cmd@\ALG@Ld @\ALG@tmp\endcsname%
621 \newcommand\algnewcommand{\ALG@defbasecmd\newcommand}%
622 \newcommand\algrenewcommand{\ALG@defbasecmd\renewcommand}%
623 \def\ALG@letcmd{\ALG@defbasecmd\let}%
624 \def\ALG@defcmd{\ALG@defbasecmd\def}%
625 \def\ALG@edefcmd{\ALG@defbasecmd\edef}%
631 \def\BState{\State \algbackskip}%
632 \def\Statex{\item[]}% an empty line
633 \newcommand\algrenewcomment{\algrenewcommand\algorithmiccomment}%
634 \def\Comment{\algorithmiccomment}%
635 \def\algref#1#2{\ref{#1}.\ref{#2}}%
636 \algnewlanguage{default}%
637 \algsetlanguage{default}%
640 % *** Line breaks ***
643 \newcommand\algbreak% for multiline parameters !!! needs fix
646 % \hskip\ALG@parindent%!!! not yet implemented
647 % \hskip-\algorithmicindent%
650 \def\ALG@noputindents%
656 % *** algorithm store / restore ***
661 \ALG@newcondstate{mustrestore}%
664 \renewcommand\ALG@beginblock%
666 \PackageError{algorithmicx}{The environment must be closed after store!}{}%
668 \@ifstar{\ALG@starstore}{\ALG@nostarstore}%
670 \def\ALG@nostarstore#1% save all infos into #1 and terminate the algorithmic block
672 \addtocounter{ALG@storecount}{1}%
673 \expandafter\global\expandafter\let\csname ALG@save@mustrestore@#1\endcsname\ALG@x@mustrestore%
676 \def\ALG@starstore#1%
678 \@ifundefined{ALG@save@line@#1}{}%
679 {\PackageError{algorithmicx}{This save name '#1' is already used!}{}}%
680 \def\ALG@savename{#1}%
681 \expandafter\xdef\csname ALG@save@totalnr@\ALG@savename\endcsname{\theALG@nested}%
682 \expandafter\xdef\csname ALG@save@line@\ALG@savename\endcsname{\theALG@line}%
683 \expandafter\xdef\csname ALG@save@numberfreq@\ALG@savename\endcsname{\ALG@numberfreq}%
684 \expandafter\xdef\csname ALG@save@rem@\ALG@savename\endcsname{\theALG@rem}%
685 \let\ALG@storerepeat\ALG@store%
688 \def\ALG@store% simply terminate all open blocks
690 \ifnum\theALG@nested=0\let\ALG@storerepeat\relax%
692 \expandafter\xdef\csname ALG@save@currentblock@\ALG@savename @\theALG@nested\endcsname%
693 {\csname ALG@currentblock@\theALG@nested\endcsname}%
694 \expandafter\ifx\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
696 \expandafter\xdef\csname ALG@save@currentlifetime@\ALG@savename @\theALG@nested\endcsname%
697 {\csname ALG@currentlifetime@\theALG@nested\endcsname}%
699 \expandafter\xdef\csname ALG@save@ind@\ALG@savename @\theALG@nested\endcsname%
700 {\csname ALG@ind@\theALG@nested\endcsname}%
710 \@ifstar{\ALG@starrestore}{\ALG@nostarrestore}%
712 \def\ALG@starrestore%
714 \let\ALG@restorerem\relax%
715 \let\ALG@restorereprem\relax%
718 \def\ALG@nostarrestore%
720 \let\ALG@restorerem\ALG@restoreremovesave%
721 \let\ALG@restorereprem\ALG@restorerepremovesave%
724 \def\ALG@restoreremovesave%
726 \expandafter\global\expandafter\let\csname ALG@save@totalnr@\ALG@savename\endcsname\relax%
727 \expandafter\global\expandafter\let\csname ALG@save@line@\ALG@savename\endcsname\relax%
728 \expandafter\global\expandafter\let\csname ALG@save@rem@\ALG@savename\endcsname\relax%
729 \expandafter\global\expandafter\let\csname ALG@save@totalnr@\ALG@savename\endcsname\relax%
730 \expandafter\global\expandafter\let\csname ALG@save@numberfreq@\ALG@savename\endcsname\relax%
732 \def\ALG@restorerepremovesave%
734 \expandafter\global\expandafter\let\csname ALG@save@currentblock@\ALG@savename @\theALG@tmpcounter\endcsname\relax%
735 \expandafter\global\expandafter\let\csname ALG@save@currentlifetime@\ALG@savename @\theALG@tmpcounter\endcsname\relax%
736 \expandafter\global\expandafter\let\csname ALG@save@currentlifetime@\ALG@savename @\theALG@tmpcounter\endcsname\relax%
737 \expandafter\global\expandafter\let\csname ALG@save@ind@\ALG@savename @\theALG@tmpcounter\endcsname\relax%
739 \def\ALG@restoremain#1% restore all infos from #1 in an open algorithmic block
741 \ifnum\theALG@line=0%
742 \else\PackageError{algorithmicx}{Restore might be used only at the beginning of the environment!}{}%
744 \def\ALG@savename{#1}%
745 \expandafter\ifx\csname ALG@save@totalnr@\ALG@savename\endcsname\relax%
746 \PackageError{algorithmicx}{Save '\ALG@savename'\space not defined!!!}{}%
748 \@ifundefined{ALG@save@mustrestore@\ALG@savename}{}%
750 \addtocounter{ALG@storecount}{-1}%
751 \expandafter\global\expandafter\let\csname ALG@save@mustrestore@\ALG@savename\endcsname\relax%
753 \setcounter{ALG@line}{\csname ALG@save@line@\ALG@savename\endcsname}%
754 \edef\ALG@numberfreq{\csname ALG@save@numberfreq@\ALG@savename\endcsname}%
755 \setcounter{ALG@rem}{\csname ALG@save@rem@\ALG@savename\endcsname}%
756 \setcounter{ALG@tmpcounter}{\csname ALG@save@totalnr@\ALG@savename\endcsname}%
757 \setcounter{ALG@nested}{0}%
759 \let\ALG@restorerepeat\ALG@restore%
764 \ifnum\theALG@tmpcounter>0%
765 \expandafter\edef\csname ALG@currentblock@\theALG@tmpcounter\endcsname%
766 {\csname ALG@save@currentblock@\ALG@savename @\theALG@tmpcounter\endcsname}%
767 \expandafter\ifx\csname ALG@save@currentlifetime@\ALG@savename @\theALG@tmpcounter\endcsname\relax%
768 \expandafter\let\csname ALG@currentlifetime@\theALG@tmpcounter\endcsname\relax%
770 \expandafter\edef\csname ALG@currentlifetime@\theALG@tmpcounter\endcsname%
771 {\csname ALG@save@currentlifetime@\ALG@savename @\theALG@tmpcounter\endcsname}%
774 \ALG@beginblock{\csname ALG@save@ind@\ALG@savename @\theALG@tmpcounter\endcsname}%
776 \addtocounter{ALG@tmpcounter}{-1}%
777 \else\let\ALG@restorerepeat\relax%
783 \ifnum\theALG@storecount>0\relax%
784 \PackageError{algorithmicx}{Some stored algorithms are not restored!}{}%