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.
This a a slight modification of How to put algorithm and figure(s) side by side?
Here is a minimal example showing how to pair algorithms side-by-side:
\documentclass{llncs}% http://www.springer.com/computer/lncs/lncs+authors?SGWID=0-40209-0-0-0
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[compatibility=false]{caption}% http://ctan.org/pkg/caption
\begin{document}
\lipsum[1]
\medskip
\noindent\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{algo1}
\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}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{algo2}
\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}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{minipage}
\medskip
On the left is Algorithm~\ref{algo1}. On the right is Algorithm~\ref{algo2}.
\lipsum[2]
\end{document}
The idea behind this approach is to have two minipage
s spanning the entire \linewidth
. These minipage
s then house the non-floating algorithmic
environments, as well as the non-floating \captionof
caption. If need be, you can wrap the entire double minipage
inside a floating algorithm
environment.
The use of geometry
was just to gain some stock real estate (may not be required in your instance), while lipsum
provided some dummy text, Lorem ipsum style.
caption
provides the means to have a caption outside of a float via \captionof
. However, it requires the compatibility=false
option to work, since llncs
already redefines \caption
- detected by caption
. algorithm
is still required, since it provides the algorithm
counter and "List of Algorithms" capability.
Best Answer
The main problem here is using floats in a
twocolumn
document. Although it is possible, your needs do not conform to their purpose. Instead, thecaption
package removes the float-y-ness of floats, allowing you to specify a caption in the text where it is called.The following MWE uses
algorithmicx
'salgpseudocode
to display an algorithm with slight variations across the three. The main interface for non-floating captions usingcaption
isSince I loaded
algorithm
,<float env>
can bealgorithm
to obtain the appropriate caption.geometry
,lipsum
were just used for layout and dummy text.There's also the stand-alone
capt-of
package which does something similar, as well asccaption
.Keeping things together in a float-like environment is possible. Wrap the set of algorithms in a
figure
(yes, afigure
, ortable
) environment while still using\captionof{algorithm}
, or just place them all in analgorithm
environment with a\caption
for each. The following shows the former:You may have to adjust the space between the
algorithmic
and\captionof
to suit your needs.