I am using the algorithmic
package to typeset an algorithm. I would like to have connecting lines between the beginning and end of a loop or conditional like in the algorithm2e
package. Is there an easy way to accomplish this, or am I better off switching to algorithm2e
?
[Tex/LaTex] Is it possible to have connecting loop lines (like algorithm2e) in algorithmic
algorithm2ealgorithms
Best Answer
Here is one way of doing it using
algorithmicx
- more advanced yet similar to and compatible withalgorithmic
. It involves expanding and contracting a token list\thelines
that keeps track of the nested level. At every new level, an additional\theline
is added to\thelines
.Every single definition is redefined to now manage the indentation:
\printindent
and append\pushindent
. This removes the regular algorithmic indent\algorithmicindent
and prints the vertical rule. At the end it adds another indent so that subsequent statements are properly indented; and\popindent\printindent
. This removes the regular algorithmic indent\algorithmicindent
, then swallows one indent, and prints the remaining indents.I've added some additional macros like
\printandpush
and\popandprint
to combine the above\printindent
,\pushindent
and\popindent
modifications.Instead of using the traditional
\State
for procedural statements, use\LState
. It performs a similar task to what is mentioned above.This solution should work well for the standard environments defined about. However, other more complicated environments defined using
\algdef
may require more fiddling to obtain correct indentation.Regarding the vertical rule. It is set at
.5em
from the left-most margin of the block, is.4pt
wide and is defined by\therule
:Token list appending is described in How does one append material to a token list?, while token removal is discussed in How does one remove material from a token list?