[Tex/LaTex] How to create vertical lines indentation in algorithm pseudo code correctly without end keywords

algorithms

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

enter image description here

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
% start with some helper code
% 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]%
    \advance \ALG@printindent@tempcnta 1
    \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.)

Best Answer

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
% start with some helper code
% 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]%
    \advance \ALG@printindent@tempcnta 1
    \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}

enter image description here