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.
[Tex/LaTex] How to format multiple algorithms so that they appear together
algorithmsspacing
Related Solutions
To break down your post, you want:
- To have an algorithm appear immediately after a paragraph in which it was discussed; and
- To have the algorithm split across pages (if possible).
In order to fulfill both requests, you cannot place your algorithmicx
environment within an algorithm
environment, since the latter is a float. And, floats move around. Moreover, floats do not break over pages. So, you'll have to make your own environment that accounts for an algorithm caption and number. Here is a very basic example of what that would entail:
\documentclass{article}
\usepackage{algorithmicx}% http://ctan.org/pkg/algorithmicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
\usepackage{needspace}% http://ctan.org/pkg/needspace
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
% ================ ALGORITHM ENVIRONMENT ================
\newcounter{myalg}% Algorithm counter
\newenvironment{myalg}[1][]%
{% \begin{myalg}[#1]
\needspace{2\baselineskip}% At least 2\baselineskip required, otherwise break
\noindent \rule{\linewidth}{1pt} \endgraf% Top rule
\refstepcounter{myalg}% For correct reference of algorithm
\centering \textsc{Algorithm}~\themyalg%
\ifthenelse{\isempty{#1}}{}{:\ #1}% Typeset name (if provided)
}{% \end{myalg}
\noindent \rule{\linewidth}{1pt}% Bottom rule
}%
\begin{document}
\lipsum[1-4]
Here is Algorithm~\ref{first-alg}:
\begin{myalg}[My first algorithm]
\label{first-alg}
\begin{algorithmic}[1]
\State This is some code
\State This is some more code
\State Here is some code
\State This is some code
\State This is some more code
\State Here is some code
\State This is some code
\State This is some more code
\State Here is some code
\State This is some code
\State This is some more code
\State Here is some code
\end{algorithmic}
\end{myalg}
That was Algorithm~\ref{first-alg}.
\lipsum[4-6]
\end{document}
In the above example, the myalg
environment does not float and replaces the original algorithm
environment. As an optional argument, myalg
will typeset a caption. The xifthen
package provides a conditional clause to check for a caption or not. The needspace
package conditions on the amount of space left on the page. In order to not be stuck with a rule or caption at the page bottom, needspace
will issue a \break
if there is less then 2\baselineskip
left on the page. The hyperref
package was also included to show you that the hyperlinks work properly, while the lipsum
package provides dummy text.
If you're into a more vibrant array of decorations for your algorithms, then the mdframed
package allows easy breaking of decorative frames across pages:
This can be extended to include items into a "List of Algorithms" as well.
The main problem here is using floats in a twocolumn
document. Although it is possible, your needs do not conform to their purpose. Instead, the caption
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
's algpseudocode
to display an algorithm with slight variations across the three. The main interface for non-floating captions using caption
is
\captionof{<float env>}{<caption>}
Since I loaded algorithm
, <float env>
can be algorithm
to obtain the appropriate caption.
\documentclass[twocolumn]{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{algorithm,algpseudocode,lipsum}% http://ctan.org/pkg/{algorithms,algorithmicx,lipsum}
\usepackage{caption}% http://ctan.org/pkg/caption
\begin{document}
\lipsum[87]
\captionof{algorithm}{Euclid’s algorithm A}\label{euclidA}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{GCD of $a$ and $b$}
\State $r_1\gets a\bmod b$
\While{$r\not=0$}\Comment{$r=0\mapsto\mbox{answer}$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile1}
\State \textbf{return} $b$\Comment{The GCD is $b$}
\EndProcedure
\end{algorithmic}
\captionof{algorithm}{Euclid’s algorithm B}\label{euclidB}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{GCD of $a$ and $b$}
\State $r_2\gets a\bmod b$
\While{$r\not=0$}\Comment{$r=0\mapsto\mbox{answer}$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile2}
\State \textbf{return} $b$\Comment{The GCD is $b$}
\EndProcedure
\end{algorithmic}
\captionof{algorithm}{Euclid’s algorithm C}\label{euclidC}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{GCD of $a$ and $b$}
\State $r_3\gets a\bmod b$
\While{$r\not=0$}\Comment{$r=0\mapsto\mbox{answer}$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile3}
\State \textbf{return} $b$\Comment{The GCD is $b$}
\EndProcedure
\end{algorithmic}
\lipsum[1-5]
\end{document}
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 as ccaption
.
Keeping things together in a float-like environment is possible. Wrap the set of algorithms in a figure
(yes, a figure
, or table
) environment while still using \captionof{algorithm}
, or just place them all in an algorithm
environment with a \caption
for each. The following shows the former:
\documentclass[twocolumn]{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{algorithm,algpseudocode,lipsum}% http://ctan.org/pkg/{algorithms,algorithmicx,lipsum}
\usepackage{caption}% http://ctan.org/pkg/caption
\begin{document}
\lipsum[1-2]
\begin{figure}[t]
\captionof{algorithm}{Euclid’s algorithm A}\label{euclidA}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{GCD of $a$ and $b$}
\State $r_1\gets a\bmod b$
\While{$r\not=0$}\Comment{$r=0\mapsto\mbox{answer}$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile1}
\State \textbf{return} $b$\Comment{The GCD is $b$}
\EndProcedure
\end{algorithmic}
\captionof{algorithm}{Euclid’s algorithm B}\label{euclidB}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{GCD of $a$ and $b$}
\State $r_2\gets a\bmod b$
\While{$r\not=0$}\Comment{$r=0\mapsto\mbox{answer}$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile2}
\State \textbf{return} $b$\Comment{The GCD is $b$}
\EndProcedure
\end{algorithmic}
\captionof{algorithm}{Euclid’s algorithm C}\label{euclidC}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{GCD of $a$ and $b$}
\State $r_3\gets a\bmod b$
\While{$r\not=0$}\Comment{$r=0\mapsto\mbox{answer}$}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile3}
\State \textbf{return} $b$\Comment{The GCD is $b$}
\EndProcedure
\end{algorithmic}
\end{figure}
\lipsum[1-5]
\end{document}
You may have to adjust the space between the algorithmic
and \captionof
to suit your needs.
Best Answer
Some not-so-optimal approaches
One attempt at keeping algorithms together would be to use the same float specifier for both floats:
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 thefloat
package. This float specifier virtually removed the float-ness of a float and places it in the text where the environment is called: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: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 thealgorithm
package. It is perfectly fine to use afigure
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 ofalgorithm
. Additionally, I've chosenalgpseudocode
(from thealgorithmicx
bundle) for typesetting the algorithm within analgorithmic
environment. Similar functionality is provided by thealgorithmic
package (also from thealgorithms
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 thick0.8pt
rule (\toprule
), followed by the caption (flush left), followed by another rule (\midrule
), taken directly from thefloat
package. Thealgorithmic
environment should be finished off with a\bottomrule
, making the approach very muchtabular
-like.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). Usingfloat
to create analgorithm
float (rather than using thealgorithm
package) is merely so one can tap into things like a\listofalgorithms
as well as analgorithm
counter.Additional packages used in the examples include
lipsum
(not required) for dummy text and theifmtarg
package (required) to check for an empty argument to\algcaption
.* The
algorithmicx
bundle provides more advanced features, including the possibility to store/restorealgorithmic
settings (that were used in the above examples), which is not offered by that of thealgorithms
bundle.