TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I recommend switching. – Marco Daniel Apr 18 '12 at 21:15
up vote 16 down vote accepted

Here is one way of doing it using algorithmicx - more advanced yet similar to and compatible with algorithmic. 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.

enter image description here

\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

% This is the vertical rule that is inserted
\def\therule{\makebox[\algorithmicindent][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newtoks\therules% Contains rules
\therules={}% Start with empty token list
\def\appendto#1#2{\expandafter#1\expandafter{\the#1#2}}% Append to token list
\def\gobblefirst#1{% Remove (first) from token list
\def\LState{\State\unskip\the\therules}% New line-state

%      ***      DECLARED LOOPS      ***
% (from algpseudocode.sty)
  {\printandpush\algorithmicwhile\ #1\ \algorithmicdo}
  {\popandprint\algorithmicend\ \algorithmicwhile}%
  {\printandpush\algorithmicfor\ #1\ \algorithmicdo}
  {\popandprint\algorithmicend\ \algorithmicfor}%
  {\printindent\algorithmicforall\ #1\ \algorithmicdo}%
  {\popandprint\algorithmicend\ \algorithmicloop}%
  {\popandprint\algorithmicuntil\ #1}%
  {\printandpush\algorithmicif\ #1\ \algorithmicthen}
  {\popandprint\algorithmicend\ \algorithmicif}%
  {\popandprint\pushindent\algorithmicelse\ \algorithmicif\ #1\ \algorithmicthen}%
   {\printandpush\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicprocedure}%
   {\printandpush\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicfunction}%


  \caption{Euclid’s algorithm}\label{euclid}
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of a and b}
      \LState $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \LState $a\gets b$
        \LState $b\gets r$
        \LState $r\gets a\bmod b$
      \LState \Return $b$\Comment{The gcd is b}

Every single definition is redefined to now manage the indentation:

  • At the start of every block, you insert \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
  • At the end of every block, you insert \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:

    \hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

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?

share|improve this answer
Nice solution!! – Gonzalo Medina Apr 21 '12 at 17:12
@GonzaloMedina: Thanks! – Werner Apr 21 '12 at 17:15
Hello, please i'd like to add lines between while and endwhile like you, this is my example \usepackage{algorithm} \usepackage{algorithmic} \begin{document} \begin{algorithm}[H] \caption{My algo} \begin{algorithmic}[1] \WHILE{condition} \STATE instruction 1 \STATE instruction 2 \ENDWHILE \end{algorithmic} \end{algorithm} \end{document} – researcher Oct 1 '12 at 15:47
should i add \usepackage{algpseudocode} ...... with \usepackage{algorithm} \usepackage{algorithmic} or should i delete \usepackage{algorithmic} ? – researcher Oct 1 '12 at 15:48
It does not seem to work properly with \ForAll. Would it be correct to use \algdef{SE}[FOR]{ForAll}{EndFor}[1] {\printandpush\algorithmicforall\ #1\ \algorithmicdo} {\popandprint\algorithmicend\ \algorithmicfor}% instead of \algdef{S}[FOR]{ForAll}[1] {\printindent\algorithmicforall\ #1\ \algorithmicdo}% ? – Magnar Myrtveit Oct 22 '14 at 22:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.