[Tex/LaTex] 3 Algorithms in the same column with algorithmic

algorithmsfloats

I have 3 algorithms that I want to be displayed in the same column of a 2-column document. They are all slight variations of each other, so it is important that they be displayed right next to each other for easy comparison.

The problem is that I can't figure out a way to place all three in the column. I've tried putting them inside a figure environment, but then I get the error "Not in outer par mode." Using another algorithm package is not an option for me.

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

enter image description here

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

enter image description here

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