# How to format multiple algorithms so that they appear together

How can I have multiple algorithms be tied together so that they all appear next to each other. Currently LaTeX places the algorithms to far from each other.

• Could you provide a bit more information- which package(s) are you using? A MWE example would be ideal – cmhughes Nov 23 '11 at 23:24
• Assuming that your algorithms are floating, then you might like to read this post – cmhughes Nov 23 '11 at 23:39
• What environment(s) are you using for your algorithms? algorithm (from algorithms) and algorithmic (from algorithmicx)? – Werner Nov 24 '11 at 0:02
• One possible solution would be to put them in a minipage. Please compose a compilable MWE that illustrates the problem. – Peter Grill Nov 24 '11 at 0:03
• Thanks for taking a look. I am using the algorithm and algorithmic package. With these I have create three \begin{algorithm}\begin{algorithmic} ... \end{algorithmic}\end{algorithm}. I wanted all the three algorithm boxes to appear together. – Sandeep Nov 24 '11 at 6:41

## 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}
\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}
\algrestore{euclidA}
\State $a\gets b$
\State $b\gets r$
\algstore{euclidB}
\end{algorithmic}
\end{algorithm}

\lipsum

\section{Second section}

\lipsum

\begin{algorithm}[t]
\caption{Third algorithm}\label{third-alg}
\begin{algorithmic}
\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

\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}[]{%
\refstepcounter{algorithm}%
\@ifmtarg{#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}
\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}
\algrestore{euclidA}
\State $a\gets b$
\State $b\gets r$
\algstore{euclidB}
\end{algorithmic}
\bottomrule
\end{figure}

\lipsum

\section{Second section}

\lipsum

\begin{algorithm}[t]
\caption{Third algorithm}\label{third-alg}
\begin{algorithmic}
\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

\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.

Between two algorithms, use the code below:

\vspace{-0.5cm}


You can set what ever number you want in {}, a negative value will shrink the spacing.

• Welcome to TeX.SX! That's a very problematic solution as it is always manual work. Maybe this is more suited as a comment than a real answer. – TeXnician Aug 20 '17 at 10:26