Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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.

share|improve this answer
    
This does not solve my problem. If you change the first \lipsum[87] into \lipsum[1-2], then the algorithms get broken up into multiple columns. I don't care if it automatically floats it away from the position of the text, I just want all three algorithms to be one after the other in the same column. –  Mike Izbicki Oct 31 '12 at 22:37
    
@MikeIzbicki wrap the three algorithms (in Werner's code, from right after \lipsum[87] up to the third \end{algorithmic}) inside a floating environment; figure, for example. –  Gonzalo Medina Oct 31 '12 at 23:03
    
@GonzaloMedina That was what I tried originally, but I get the "Not in outer par mode" error. –  Mike Izbicki Oct 31 '12 at 23:05
    
@MikeIzbicki this is because you are using \caption; notice that Werner's example uses \captionof from the caption package. –  Gonzalo Medina Oct 31 '12 at 23:06
    
@GonzaloMedina It's not the \caption that was breaking it, but the algorithm environment that I was putting it into that Werner's example didn't put it into (I get the same error even without a caption). Is there any way I can still get that environment's aesthetics with only using \captionof? –  Mike Izbicki Oct 31 '12 at 23:19

It turns out that you can convert an algorithm into a non-float by appending an [H] at the end of it. This code works for what I wanted to do:

\begin{figure}

\begin{algorithm}[H]
\caption{Alg 1}
\begin{algorithmic}
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
\caption{Alg 1}
\begin{algorithmic}
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
\caption{Alg 1}
\begin{algorithmic}
\end{algorithmic}
\end{algorithm}

\end{figure}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.