How to format multiple algorithms so that they appear together
Some not-so-optimal approaches
One attempt at keeping algorithms together would be to use the same float specifier for both floats:
%...
\begin{algorithm}[t]% First algorithm
%...
\end{algorithm}
\begin{algorithm}[t]% Second algorithm
%...
\end{algorithm}
%...
LaTeX will ensure that they follow in sequence since the floats are managed by a list that is flushed in a FIFO (first-in-first-out) manner. However, this is very unreliable since float placement is dependent on the number of floats already on the page, as well as the remaining space on a page. So, algorithms could be broken fairly easily in this way.
Another attempt would be to use the H
float specifier supplied by the float
package. This float specifier virtually removed the float-ness of a float and places it in the text where the environment is called:
%...
\begin{algorithm}[H]% First algorithm
%...
\end{algorithm}
\begin{algorithm}[H]% Second algorithm
%...
\end{algorithm}
%...
Although this works pretty well, you lose the mobility offered by a float, so algorithms will appear where you put them. Moreover, this approach is not perfect around page boundaries, where the two algorithms could be separated. Moreover, the effects could be worse if using the H
float specifier for some algorithms and other (regular) float specifiers for other non-stick-together algorithms. Here is a small example that illustrates this problematic instance:
\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{float}% http://ctan.org/pkg/float
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\begin{document}
\listofalgorithms
\section{First section}
\lipsum[1-2]
\begin{algorithm}[H]
\caption{First algorithm}\label{first-alg}%
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\algstore{euclidA}
\end{algorithmic}
\end{algorithm}
\begin{algorithm}[H]
\caption{Second algorithm}\label{second-alg}
\begin{algorithmic}[1]
\algrestore{euclidA}
\State $a\gets b$
\State $b\gets r$
\algstore{euclidB}
\end{algorithmic}
\end{algorithm}
\lipsum[2]
\section{Second section}
\lipsum[3]
\begin{algorithm}[t]
\caption{Third algorithm}\label{third-alg}
\begin{algorithmic}[1]
\algrestore{euclidB}
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\lipsum[4]
\end{document}
The example has been chosen specifically to break the two algorithms that are supposed to stick together across the first page boundary, while the third algorithm is left to float. As such, the sequence of algorithms displayed in the output is jumbled: Algorithm 1 is followed by Algorithm 3, which is followed by Algorithm 2:
Not only that, as can be seen from the output (click to enlarge), the order in the "List of Algorithms" is also incorrect.
A slightly manual (but functional) approach
The idea is to use an existing float (like figure
) as a float wrapper and define some commands that mimic the captioning and typesetting behaviour of the algorithm
package. It is perfectly fine to use a figure
as a float wrapper, just as long as there is no "offical \caption
" used inside (which will typeset a figure caption, and would eventually make its way into the LoF).
I've decided to use the ruled
float style of algorithm
. Additionally, I've chosen algpseudocode
(from the algorithmicx
bundle) for typesetting the algorithm within an algorithmic
environment. Similar functionality is provided by the algorithmic
package (also from the algorithms
bundle), so the example is transferable*.
In order to mimic the captioning style of ruled
, a new macro \algcaption[<LoA>]{<caption>}
should be used. This typesets a thick 0.8pt
rule (\toprule
), followed by the caption (flush left), followed by another rule (\midrule
), taken directly from the float
package. The algorithmic
environment should be finished off with a \bottomrule
, making the approach very much tabular
-like.
\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{ifmtarg}% http://ctan.org/pkg/ifmtarg
\makeatletter
\newcommand{\listofalgorithms}{\listof{algorithm}{List of Algorithms}}%
\newcommand{\toprule}{\hrule height.8pt depth0pt \kern2pt} % Caption top horizontal rule+skip
\newcommand{\midrule}{\kern2pt\hrule\kern2pt} % Caption bottom (or mid) horizontal rule+skip
\newcommand{\bottomrule}{\kern2pt\hrule\relax}% Algorithm bottom rule
\newcommand{\algcaption}[2][]{%
\refstepcounter{algorithm}%
\@ifmtarg{#1}
{\addcontentsline{loa}{figure}{\protect\numberline{\thealgorithm}{\ignorespaces #2}}}
{\addcontentsline{loa}{figure}{\protect\numberline{\thealgorithm}{\ignorespaces #1}}}%
\toprule
\textbf{\fname@algorithm~\thealgorithm}\ #2\par % Caption
\midrule
}
\makeatother
\begin{document}
\listofalgorithms
\section{First section}
\lipsum[1-3]
\begin{figure}[ht]
\algcaption{First algorithm}\label{first-alg}%
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\algstore{euclidA}
\end{algorithmic}
\bottomrule
\bigskip
\algcaption{Second algorithm}\label{second-alg}
\begin{algorithmic}[1]
\algrestore{euclidA}
\State $a\gets b$
\State $b\gets r$
\algstore{euclidB}
\end{algorithmic}
\bottomrule
\end{figure}
\lipsum[2]
\section{Second section}
\lipsum[3]
\begin{algorithm}[t]
\caption{Third algorithm}\label{third-alg}
\begin{algorithmic}[1]
\algrestore{euclidB}
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\lipsum[4]
\end{document}
Algorithms meant to stick together this way are contained within the same wrapper float (figure
in this case) and are separated using \bigskip
(which roughly match the space between other floats). Using float
to create an algorithm
float (rather than using the algorithm
package) is merely so one can tap into things like a \listofalgorithms
as well as an algorithm
counter.
Additional packages used in the examples include lipsum
(not required) for dummy text and the ifmtarg
package (required) to check for an empty argument to \algcaption
.
* The algorithmicx
bundle provides more advanced features, including the possibility to store/restore algorithmic
settings (that were used in the above examples), which is not offered by that of the algorithms
bundle.