I am making a two parts splited algorithm with vertical lines and numbering. I prefer to use algorithm package rather than using algorithm2e package, because this one does not provid either \algstore and \algrestore command pair or some easy solution about vertical lines and numerating, when algorithm is broken in two parts. Because of that, based on solution of issue "How can I create vertical lines indentation in algorithm pseudo code correctly without end keywords?", I am breaking this algorithm in two parts, using those algorithm command pair. It is working fine, according this figure:
But, when I use a long text, two problems (red lines) are appearing:
- The long text exceds left margin, which is interrupting the three vertical lines continuation, according to one sees at line 4;
- Breaking of vertical alignment from line that is below of both second for and first if, according to lines 2 and 3 (compare with lines 1 and 6), respectively.
Therfore, I would like to achieve the follow solutions:
- Compress the width of long text in justify alignment, without both interrupting the vertical lines;
- Correct the vertical alignment errors of lines that are starting below of both second for and first if (blue lines for both for and if references).
So, how can I do that?
Here, I put the figure MWE code.
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{etoolbox}
\usepackage{lipsum}
\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
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\begin{document}
% ---> Algorithm 01.
\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. \lipsum[1]
\Else
\If{$i=j+1$}
\State Stay silent
\Else
\State Break
\algstore{bkbreak}
\end{algorithmic}
\end{algorithm}
% ---> Algorithm 01: continuation.
\begin{algorithm}
\begin{algorithmic}
\algrestore{bkbreak}
\EndIf
\EndIf
\EndFor
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}

