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?

  • 3
    I recommend switching. Apr 18, 2012 at 21:15

1 Answer 1


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?

  • 2
    Nice solution!! Apr 21, 2012 at 17:12
  • 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, 2012 at 15:47
  • should i add \usepackage{algpseudocode} ...... with \usepackage{algorithm} \usepackage{algorithmic} or should i delete \usepackage{algorithmic} ?
    – researcher
    Oct 1, 2012 at 15:48
  • @researcher: The code in my answer was written for algpseudocode from algorithmicx. So you need \usepackage{algorithm}\usepackage{algpseudocode} and then change all the \STATE to \State, \WHILE to \While, \ENDWHILE to \EndWhile. I'll see what needs to be done when you want to use algcompatible, which seems to be the code you're using. Once I have that covered, I'll add it to my answer.
    – Werner
    Oct 1, 2012 at 16:17
  • 2
    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}% ? Oct 22, 2014 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .