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}


\strutand\showoutputcommands do? I also tested this improvement with another tex code. I noted that there are some short interruptions among vertical lines, whenever it did transiction between any command statement (for instance\If,\Repeat,\ForAllamong others) and\StateParcommand statement. But, either among the own\StateParcommand statements or from\StateParto another command statement, there are no short interruptions. What could it be happening? – Adriano Jan 19 '20 at 01:17\showoutputwas left over from debugging. I've removed it. Can you provide a minimal example showing some of you remaining issues? You can put the code on Pastebin if you wish. – Werner Jan 19 '20 at 02:26\offinterlineskipas part of\ALG@beginalgorithmic, so there's no more need for\vspace{-\lineskip}as part of\StatePar. – Werner Jan 21 '20 at 17:45\renewcommand{\ALG@beginalgorithmic}{\offinterlineskip}% Remove all interline skips, and I also have taken\vspace{-lineskip}out of\StateParstatement, according to your correction. As a consequence, your algorithm package patch is perfectly working right now, as way as I am wanting. Thank you by help. – Adriano Jan 21 '20 at 19:03setspace? – Werner Nov 30 '21 at 23:26\linespread, or usesetspacewith a small adjustment to condense the text. – Werner Dec 02 '21 at 16:53