# How can I create vertical lines indentation in algorithm pseudo code correctly without end keywords?

I found this answer and I applied it but this is the result.

This is my code.

\documentclass[conference]{IEEEtran}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{dsfont}
\usepackage{algorithm}
\PassOptionsToPackage{noend}{algpseudocode}% comment out if want end's to show
\usepackage{algpseudocode}

\makeatletter
% This is the vertical rule that is inserted
\newcommand*{\algrule}[1][\algorithmicindent]{\makebox[#1][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newcount\ALG@printindent@tempcnta
\def\ALG@printindent{%
\ifnum \theALG@nested>0% is there anything to print
\ifx\ALG@text\ALG@x@notext% is this an end group without any text?
% do nothing
\addvspace{-3pt}% FUDGE for cases where no text is shown, to make the rules line up
\else
\unskip
% draw a rule for each indent level
\ALG@printindent@tempcnta=1
\loop
\algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
\ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax% can't do <=, so add one to RHS and use < instead
\repeat
\fi
\fi
}%
\usepackage{etoolbox}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother
% end vertical rule patch for algorithmicx
\makeatother

\begin{document}

\IEEEpeerreviewmaketitle

\begin{algorithm}
\caption{Arbitrary Algorithm}\label{IS2OSLS}
\begin{algorithmic}[1]
\Require A matrix $\mathbf{A}$ of size $m\times n$.
\Ensure Something.
\For{$i$ in $m$}
\For{$j$ in $n$}
\If{$i=j$}
\State Select a random action
\Else
\If{$i=j+1$}
\State Stay silent
\Else
\State Break
\EndIf
\EndIf
\EndFor
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}


I wold like to have vertical lines that start just after the first letter of the keyword, i.e., for the keyword for the vertical line should start after f, etc. In the figure above, the vertical lines start after the o not the f. Also I need the pseudo code without the end keyword as shown.

Thank you.

EDIT

I can change \hspace*{.5em} to \hspace*{.1em} in order to move the vertical line a little to the left. This is solved. But why do the lines overlaps at the end of the algorithm? (If I add the end keywords the problem is solved.)

An \addvspace instruction that is the cause for overlapping; remove it. I also added another patch that avoids spurious vertical space when an “end” tag is omitted, see Spurious whitespace with algpseudocode and noend

For moving left the rules, act on the \hspace{.5em}, as you discovered; in the example I used .2em.

In the following code I left only the necessary packages.

\documentclass[conference]{IEEEtran}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{etoolbox}

\makeatletter
% This is the vertical rule that is inserted
\newcommand*{\algrule}[1][\algorithmicindent]{%
\makebox[#1][l]{%
\hspace*{.2em}% <------------- This is where the rule starts from
\vrule height .75\baselineskip depth .25\baselineskip
}
}

\newcount\ALG@printindent@tempcnta
\def\ALG@printindent{%
\ifnum \theALG@nested>0% is there anything to print
\ifx\ALG@text\ALG@x@notext% is this an end group without any text?
% do nothing
\else
\unskip
% draw a rule for each indent level
\ALG@printindent@tempcnta=1
\loop
\algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
\ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax
\repeat
\fi
\fi
}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\patchcmd{\ALG@doentity}{\item[]\nointerlineskip}{}{}{} % no spurious vertical space
% end vertical rule patch for algorithmicx
\makeatother

\begin{document}

\IEEEpeerreviewmaketitle

\begin{algorithm}
\caption{Arbitrary Algorithm}\label{IS2OSLS}

\begin{algorithmic}[1]
\Require A matrix $\mathbf{A}$ of size $m\times n$.
\Ensure Something.
\For{$i$ in $m$}
\For{$j$ in $n$}
\If{$i=j$}
\State Select a random action
\Else
\If{$i=j+1$}
\State Stay silent
\Else
\State Break
\EndIf
\EndIf
\EndFor
\EndFor
\end{algorithmic}

\end{algorithm}

\end{document}


• Thank you. Is there an easy way to change the color of the vertical lines (like, gray more transparent)? For example, this color #c2c2c2 Color Hex Gray76. – Chiba Feb 14 '16 at 23:38
• @Chiba Yes, of course: add \color{whatever} just before \vrule. – egreg Feb 15 '16 at 0:07
• For some reason, the vertical rules are sometimes dashed like | | | (in vertical fashion). I mean there is a little space like every 1 cm. What would I get this? – Chiba Mar 29 '16 at 19:26
• @Chiba I suspect you're using setspace – egreg Mar 29 '16 at 19:28
• @Chiba Sorry, but if I add that call to the code above, I get solid rules. Please, ask a new question with a minimal example showing the issue. – egreg Mar 29 '16 at 19:45