[Tex/LaTex] indentation scope lines broken in algpseudocode

algorithmsalgpseudocode

I am trying to provide a vertical line that shows the indentation in an algorithm. I'm following this answer, but when I add some math that is higher than the line height, the lines get broken. This is the result and I also provide the code. Can this issue be fixed?

enter image description here

\documentclass{article}

\usepackage{amsmath}                              % For \eqref
\usepackage{amssymb}                             % For mathematical symbols
\usepackage{amsbsy}                                % For bold symbol greek letters

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


\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

% for bmatrix with alignment
\makeatletter
  \renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
    \hskip -\arraycolsep
    \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother



\begin{document}

   \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$}
                   \State{$\mathbf{X} \gets \begin{bmatrix}[rrr]  \boldsymbol x_i  & \boldsymbol x_j & \ldots \end{bmatrix}^\intercal $} \Comment{get coordinates}

                \For{$j$ in $n$}
                    \If{$i=j$}
                        \State Select a random action
                    \Else
                        \If{$i=j^3+1$}
                            \State Stay silent 
                            \State $j \gets \begin{cases} a & b \\ c & d \end{cases} $
                        \Else 
                            \State $i \gets \mathbf{J}^\intercal$
                            \State Break
                        \EndIf
                    \EndIf
                \EndFor
            \EndFor
        \end{algorithmic}
    \end{algorithm}

\end{document}

UPDATE

Based on your code, I give another example where you can see that the rules overlap the return statement.

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsbsy}

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

\newcommand*{\algrule}[1][\algorithmicindent]{\hspace*{.5em}\vrule\vrule
width 0pt height .75\baselineskip depth .25\baselineskip\hspace*{\dimexpr#1-.5em}}

\makeatletter
\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}
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother

\AtBeginEnvironment{algorithmic}{\lineskip0pt}

\newcommand*\Let[2]{\State #1 $\gets$ #2}


\begin{document}

\begin{algorithm}
  \caption{Test 2
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{$\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ and  $\mathcal{G}$}
    \Statex
    \Function{test}{$\mathcal{M}, \mathcal{G}$}
      \Let{$\mathcal{S}$}{$\emptyset$} 
      \For{$e_i \in \mathcal{E}$}
        \For{$g_i \in \mathcal{G}$}
          \Let{$\mathcal{I}$}{$e_i  \cap g_i$}
          \If{$\mathcal{I} \neq \emptyset $} 
            \Let{$\left\{n_k \right\}$}{$\mathcal{I}$} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{S}}$}{$\mathcal{S} \bigcup_k \left\{ n_k, s_k\right\} $}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{N}}$}{$\mathcal{N} \bigcup_k \left\{n_k \right\}$}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{T}}$}{subdivide($e_i, \mathcal{I}$)} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \mathcal{T}$}  
          \EndIf
        \EndFor
      \EndFor
      \State \Return{$\mathcal{W}, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

Which gives

enter image description here

UPDATE 2

The new code is

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsbsy}

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

\newcommand*{\algrule}[1][\algorithmicindent]{\hspace*{.5em}\vrule\vrule
width 0pt height \baselineskip depth .25\baselineskip\hspace*{\dimexpr#1-.5em}}

\makeatletter
\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% can't do <=, so add one to RHS and use < instead
    \repeat
    \fi
    \fi
}%
\usepackage{etoolbox}
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother

\AtBeginEnvironment{algorithmic}{\lineskip0pt}

\newcommand*\Let[2]{\State #1 $\gets$ #2}


\algtext*{EndWhile}% Remove "end while" text
\algtext*{EndFor}% Remove "end for" text
\algtext*{EndIf}% Remove "end if" text
\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

% increase line spacing for algorithmicx
\usepackage{etoolbox}
\makeatletter
\expandafter\patchcmd\csname\string\algorithmic\endcsname{\itemsep\z@}
{\itemsep=0.2ex plus2pt}{}{}
\makeatother

\begin{document}

\showoutput

\begin{algorithm}
  \caption{Test 2
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{$\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ and  $\mathcal{G}$}
    \Statex
    \Function{test}{$\mathcal{M}, \mathcal{G}$}
      \Let{$\mathcal{S}$}{$\emptyset$} 
      \For{$e_i \in \mathcal{E}$}
        \For{$g_i \in \mathcal{G}$}
          \Let{$\mathcal{I}$}{$e_i  \cap g_i$}
          \If{$\mathcal{I} \neq \emptyset $} 
            \Let{$\left\{n_k \right\}$}{$\mathcal{I}$} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{S}}$}{$\mathcal{S} \bigcup_k \left\{ n_k, s_k\right\} $}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{ \mathcal{N}}$}{$\mathcal{N} \bigcup_k \left\{n_k \right\}$}
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{T}}$}{subdivide($e_i, \mathcal{I}$)} 
            \Let{$\phantom{\left\{n_k \right\} }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \mathcal{T}$}  
          \EndIf
        \EndFor
      \EndFor
      \State \Return{$\mathcal{W}, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}


\end{document}

which gives

enter image description here

Best Answer

Here is a solution building on the posted code. A completely different approach using tikz is to be found in Problems with vertical lines in algorithmicx.

To modify the current code you need to use rules that do not have a fixed height and depth. One way to achieve that is simply to write \vrule. However, you should ensure minimum sizes and need to make sure that the resulting lines do not get moved apart by the \lineskip mechanism. The code below covers all of your examples.

Sample output

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsbsy}

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

\usepackage{etoolbox}

\newcommand{\algruledefaultfactor}{.75}
\newcommand{\algstrut}[1][\algruledefaultfactor]{\vrule width 0pt
depth .25\baselineskip height #1\baselineskip\relax}
\newcommand*{\algrule}[1][\algorithmicindent]{\hspace*{.5em}\vrule\algstrut
\hspace*{\dimexpr#1-.5em}}

\makeatletter
\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% can't do <=, so add one to RHS and use < instead
    \repeat
    \fi
    \fi
}%

\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}

\AtBeginEnvironment{algorithmic}{\lineskip0pt}

\newcommand*\Let[2]{\State #1 $\gets$ #2}
\newcommand*\Stateh{\State \algstrut[1]}

\begin{document}

\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$}
      \State{$\mathbf{X} \gets \begin{bmatrix*}[r]  \boldsymbol x_i  & \boldsymbol x_j & \ldots \end{bmatrix*}^\intercal $} \Comment{get coordinates}
      \For{$j$ in $n$}
        \If{$i=j$}
          \State Select a random action
          \Else
          \If{$i=j^3+1$}
            \State Stay silent
            \State $j \gets \begin{cases} a & b \\ c & d \end{cases} $
            \Else
            \State $i \gets \mathbf{J}^\intercal$
            \State Break
          \EndIf
        \EndIf
        \EndFor
    \EndFor
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}
  \caption{Test 2
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{$\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ and  $\mathcal{G}$}
    \Function{test}{$\mathcal{M}, \mathcal{G}$}
      \Let{$\mathcal{S}$}{$\emptyset$}
      \For{$e_i \in \mathcal{E}$}
        \For{$g_i \in \mathcal{G}$}
          \Let{$\mathcal{I}$}{$e_i  \cap g_i$}
          \If{$\mathcal{I} \neq \emptyset $}
            \Let{$\left\{n_k \right\}$}{$\mathcal{I}$}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{ \mathcal{S}}$}{$\mathcal{S} \bigcup_k \left\{ n_k, s_k\right\} $}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{ \mathcal{N}}$}{$\mathcal{N} \bigcup_k \left\{n_k \right\}$}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{\mathcal{T}}$}{subdivide($e_i, \mathcal{I}$)}
            \Let{$\hphantom{\left\{n_k \right\} }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \mathcal{T}$}
            \EndIf
        \EndFor
      \EndFor
      \Stateh \Return{$\mathcal{W}, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

I have rewritten the \algrule command, unpacking it from a box, so an isolated \vrule can fill the height and depth of the current line. A rule of zero width is then drawn to force that height and depth to be reasonable on standard lines. This rule is encapsulated in a newcommand \algstrut. Finally the algorithmic environment is patched to have \lineskip set to 0pt.

If you have two consecutive high lines these may now touch, but then you can fix this by adding a zero width rule of suitable height and depth to your text in these one-off cases. This can be done using \algstrut[1] or some large factor on the given line.

It also turns out to be necessary to have a taller strut on \State lines, so I have provided a new command \Stateh that includes this.

Incidentally I have removed the code for the bmatrix alignment, as the mathtools package now provides bmatrix* which has an alignment option. Also I have use \hphantom instead of \phantom in your example, as you are only interested in adjust the horizontal spacing.

Related Question