I want to do something like this (see image below)
where I can describe my algorithm in smaller steps or "phases" as this person calls them. I've used the algorithm and algorithmic package before but I can't figure out how to do these "sub-algorithms" and i cant seem to find it in any tutorial.
[Tex/LaTex] Creating sub-algorithms in latex using the algorithm package
algorithmspackages
Related Solutions
There doesn’t seem to be a way of defining procedures in algorithms
but you can use an algorithm
environment with an appropriate \caption
that names your algorithm.
Personally, though, I would switch to the superior algorithmicx
package that defines a \Procedure
macro:
\begin{algorithm}
\Procedure{recurse}{$x$}
\If{$x=1$}
\State\Return{$1$}
\Else
\State\Return{\Call{recurse}{$x-1$}}
\EndIf
\EndProcedure
\end{algorithm}
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
You can use the
algpseudocode
package and define a simple command to let you format the phases; a simple example: