2

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:

enter image description here

But, when I use a long text, two problems (red lines) are appearing:

  1. The long text exceds left margin, which is interrupting the three vertical lines continuation, according to one sees at line 4;
  2. 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:

  1. Compress the width of long text in justify alignment, without both interrupting the vertical lines;
  2. 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}
2

The following updates the answer in How can I create vertical lines indentation in algorithm pseudo code correctly without end keywords?. Here are the key elements:

  • Use a \StatePar to enter a \State paragraph. This adds a \strut and the start and end of the paragraph to allow for proper baseline skip.
  • The algorithmic environment has been updated to start with \offinterlineskip. This removes the gap between each statement that is usually present in the vertical rules.
  • All keywords and the \State command have been updated to use a \strut to allow for a better presentation with vertical spacing (also due to \offinterlineskip).

enter image description here

\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]{%
  \hspace*{.2em}% <------------- This is where the rule starts from
  \vrule %height .75\baselineskip depth .25\baselineskip
  \hspace*{\dimexpr#1-.2em-.4pt}%
}

\newcommand{\StatePar}[1]{%
  \State\parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\strut #1\strut}%
}
\renewcommand{\ALG@beginalgorithmic}{\offinterlineskip}% Remove all interline skips

\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}}
% end vertical rule patch for algorithmicx
\makeatother

% Add \struts to keywords
\algrenewcommand\algorithmicend{\strut\textbf{end}}
\algrenewcommand\algorithmicdo{\strut\textbf{do}}
\algrenewcommand\algorithmicwhile{\strut\textbf{while}}
\algrenewcommand\algorithmicfor{\strut\textbf{for}}
\algrenewcommand\algorithmicforall{\strut\textbf{for all}}
\algrenewcommand\algorithmicloop{\strut\textbf{loop}}
\algrenewcommand\algorithmicrepeat{\strut\textbf{repeat}}
\algrenewcommand\algorithmicuntil{\strut\textbf{until}}
\algrenewcommand\algorithmicprocedure{\strut\textbf{procedure}}
\algrenewcommand\algorithmicfunction{\strut\textbf{function}}
\algrenewcommand\algorithmicif{\strut\textbf{if}}
\algrenewcommand\algorithmicthen{\strut\textbf{then}}
\algrenewcommand\algorithmicelse{\strut\textbf{else}}

\algrenewcommand\algorithmicrequire{\strut\textbf{Input:}}
\algrenewcommand\algorithmicensure{\strut\textbf{Output:}}

\let\oldState\State
\renewcommand{\State}{\oldState\strut}

\begin{document}

% ---> Algorithm 01.
\begin{algorithm}
  \caption{Arbitrary Algorithm}
  \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$}
          \StatePar{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}
  • Yes, it is exactly what I am wanting. Thank you by help. But I have a question: what do \strut and \showoutput commands 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, \ForAll among others) and \StatePar command statement. But, either among the own \StatePar command statements or from \StatePar to another command statement, there are no short interruptions. What could it be happening? – Adriano Jan 19 at 1:17
  • Those short interruptions are as in the issue Why are vertical rules dashed sometimes with algorithmic package?. But in your solution, I noted that you did a short adjust in \vrule command. Does it influence in those short interruptions? – Adriano Jan 19 at 1:20
  • @Adriano: \showoutput was 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 at 2:26
  • I made a MWE with those problems using the same code example, according to your suggestion. This is avaliable in Pastebin, through of issue LaTeX MWE refering to vertical line on algorithm package. I am using LaTeX elsarticle class, because those problems are appearing in Elsevier article mask. This is avaliable in Elsevier LaTeX instructions. It seems that problems are in mathematical symbol sizes, because short interruptions are noted in the lines that appear. – Adriano Jan 21 at 13:52
  • 1
    @Adriano: I've updated the answer to correct the issue. It now sets \offinterlineskip as part of \ALG@beginalgorithmic, so there's no more need for \vspace{-\lineskip} as part of \StatePar. – Werner Jan 21 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.