8

I use the algorithm float environment for pseudocode and want some algorithms to span two pages. That works fine with \algstore and \algrestore. However, I would like to omit these borders at the end of the first part of the algorithm and at the top of the continuing part.

Is there a way to remove them while keeping the remaining border lines?

What I have is something like:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algcompatible}

\newcommand{\algoallowbreak}{\algstore{myalg}\end{algorithmic}\end{algorithm}
    \begin{algorithm}
    \begin{algorithmic}\algrestore{myalg}
}

\begin{document}

\begin{algorithm}[h]
\caption{Algorithm 1}
\begin{algorithmic}[1]
    \State $a \gets b$
    \State some other code

\algoallowbreak

    \State some code that could possibly be wrap onto the next side
\end{algorithmic}
\end{algorithm}

\end{document}

And I want to remove the both border lines between algorithm line 2 and 3.

enter image description here

Werner
  • 603,163
Tobel
  • 83

2 Answers2

4

Here's one possible solution; it requires for you to issue \BreakAlgo before an algorithmth that will have the new specifications:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algcompatible}
\usepackage{etoolbox}

\makeatletter
\newcommand\fs@myruledtop{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
  \def\@fs@post{\kern2pt}%
  \def\@fs@mid{\kern2pt\hrule\kern2pt}%
  \let\@fs@iftopcapt\iftrue}
\newcommand\fs@myruledbottom{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{\kern2pt}%
  \def\@fs@post{\kern2pt\hrule\relax}%
  \def\@fs@mid{}%
  \let\@fs@iftopcapt\iftrue}
\makeatother

\newif\ifalgobreak
\newif\ifalgobreaksecond

\newcommand\BreakAlgo{\algobreaktrue}

\AtBeginEnvironment{algorithm}{
\ifalgobreak
  \floatstyle{myruledtop}
  \restylefloat{algorithm}
\fi
\ifalgobreaksecond
  \floatstyle{myruledbottom}
  \restylefloat{algorithm}
\else
\fi
}

\AfterEndEnvironment{algorithm}{%
  \algobreakfalse\algobreaksecondfalse
  \floatstyle{ruled}
  \restylefloat{algorithm}%
}

\newcommand\algoallowbreak[1][]{%
  \algstore{myalg}
  \end{algorithmic}
  \end{algorithm}
  \algobreaksecondtrue
  \begin{algorithm}
  \begin{algorithmic}[#1]
  \algrestore{myalg}%
}

\begin{document}

\BreakAlgo
\begin{algorithm}[h]
\caption{Algorithm 1}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code

\algoallowbreak

\State some code that could possibly be wrap onto the next side
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[h]
\caption{Algorithm 2}
\begin{algorithmic}[1]
    \State $a \gets b$
    \State some other code
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

The idea was to define two new styles for algorithms (one suppressing the bottom rule, the other one, suppressing the top rule) and applying these new styles conditionally depending on \BreakAlgo

I also added an optional argument to \algoallowbreak so as to specify numbering in the continuation part; notice that this fixes the alignment problem you had in your original settings for the continuation. If the numbering will always be present, you don't need the optional argument and in this case the definition would be

\newcommand\algoallowbreak{%
  \algstore{myalg}
  \end{algorithmic}
  \end{algorithm}
  \algobreaksecondtrue
  \begin{algorithm}
  \begin{algorithmic}[1]
  \algrestore{myalg}%
}

And then, in your document:

\BreakAlgo
\begin{algorithm}[h]
\caption{Algorithm 1}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code

\algoallowbreak

\State some code that could possibly be wrap onto the next side
\end{algorithmic}
\end{algorithm}
Gonzalo Medina
  • 505,128
2

I suspect the only reason you want this smooth break is because non-floating algorithm that you wish to break across a page. For that, I've created a nofloatalgorithmic environment that takes an optional argument for the line numbering (just like the regular algorithmic environment does) and a mandatory argument for the algorithm caption*.

enter image description here

\documentclass{article}
\usepackage[paper=a6paper]{geometry}% Just for this example
\usepackage{algorithm,needspace}
\usepackage{algcompatible}

\makeatletter
\newenvironment{nofloatalgorithmic}[2][0]
  {% \begin{nofloatalgorithmic}
  \par
  \addvspace{\intextsep}% Vertical gap above in-text float
  \needspace{\dimexpr\baselineskip+6.8pt}%
  \noindent%
  \hrule height.8pt depth0pt \kern2pt% Top horizontal rule
  \refstepcounter{algorithm}% Step algorithm counter
  \addcontentsline{loa}{algorithm}{\numberline{\thealgorithm}#2}% Add entry to LoA
  \noindent\textbf{\fname@algorithm~\thealgorithm} #2\par% Write caption
  \kern2pt\hrule\kern2pt% Middle horizontal rule
  \begin{algorithmic}[#1]
  }
  {% \end{nofloatalgorithmic}
  \end{algorithmic}
  \nobreak\kern2pt\hrule\relax% Bottom horizontal rule
  \addvspace{\intextsep}% Vertical gap below in-text float
  }
\makeatother

\begin{document}
\listofalgorithms
\begin{nofloatalgorithmic}[1]{Some algorithm}
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
  \State $a \gets b$
  \State some other code
  \State some code that could possibly be wrap onto the next side
\end{nofloatalgorithmic}

\begin{algorithm}[h]
  \caption{Another algorithm}
  \begin{algorithmic}[1]
      \State $a \gets b$
      \State some other code
  \end{algorithmic}
\end{algorithm}
\end{document}

The caption is generated in the same way the algorithm package does it. In fact, algorithm uses float to set the floating algorithm in a ruled style which uses the following "pre", "mid" and "post" definitions for the caption:

\newcommand\fs@ruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
  \def\@fs@post{\kern2pt\hrule\relax}%
  \def\@fs@mid{\kern2pt\hrule\kern2pt}%
  \let\@fs@iftopcapt\iftrue}

needspace makes sure the new caption sticks to the actual page-breakable list (that is algorithmic).

Finally, since this is set as an in-text float, a gap above and below of \intextsep sets it apart form the regular paragraph text in true float-like fashion, as suggested by this view of lengths used in float (from the layouts documentation):

enter image description here

Ps. I've not tested this extensively to make sure page breaks don't screw/split up the horizontal rules, so let me know if there are problems.

*This definition can be extended to take an additional optional argument that could be used for a different LoA-style entry.

Werner
  • 603,163