[Tex/LaTex] algorithm with partial borders/frames

algorithmicxalgorithmspage-breakingrules

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

Best Answer

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}