# Place four algorithms (algorithm2e) in a subfigure and arrange them in two rows and two columns

I want to arrange four algorithms set with the algorithm2e package in two rows and two columns, as shown in the figure below. I've tried the subfigure package and have also read several related questions and answers, but have so far been unable to produce an output as shown below.

|------------------|       |-------------------|
|                  |       |                   |
|                  |       |                   |
|                  |       |                   |
|                  |       |                   |
|------------------|       |-------------------|
(a) ...                       (b) ...

|------------------|       |-------------------|
|                  |       |                   |
|                  |       |                   |
|                  |       |                   |
|                  |       |                   |
|------------------|       |-------------------|
(c) ...                       (d) ...

Figure 1: ...

-
–  Marco Daniel Jan 7 '12 at 20:20

You can use the subfigure environment from the subcaption package as follows.

Note that the subfigure environment takes the same optional arguments that minipage does, so if you find yourself in a situation with different size code snippets you can use (for example) \begin{subfigure}[t]{.5\textwidth}...

\documentclass{article}
\usepackage{algorithm2e}
\usepackage{subcaption}

\newcommand{\myalgorithm}{%
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\end{algorithm}}
\begin{document}

\begin{figure}
\begin{subfigure}{.5\textwidth}
\myalgorithm
\caption{How to write algorithms}
\end{subfigure}% need this comment symbol to avoid overfull hbox
\begin{subfigure}{.5\textwidth}
\myalgorithm
\caption{How to write algorithms}
\end{subfigure}\\
\begin{subfigure}{.5\textwidth}
\myalgorithm
\caption{How to write algorithms}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
\myalgorithm
\caption{How to write algorithms}
\end{subfigure}
\caption{Main caption}
\end{figure}

\end{document}

-

Here is an option - it requires you to wrap each algorithm inside a minipage.

\documentclass{article}
\usepackage[a3paper,landscape]{geometry}% http://ctan.org/pkg/geometry
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\newcommand{\myalg}[1]{%
\begin{minipage}{.4\linewidth}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }

initialization\;
\While{not at end of this document}{
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}%
\caption{How to write algorithms}%
\label{#1}%
\end{algorithm}%
\end{minipage}%
}
\begin{document}

\begin{figure}[htb]
\null\hfill \myalg{alg1} \hfill \myalg{alg2} \hfill\null\par \medskip
\null\hfill \myalg{alg3} \hfill \myalg{alg4} \hfill\null
\caption{Here are some algorithms.}
\end{figure}
\end{document}


In the above example and for the sake of brevity, I've made the command \myalg{<lab>} typeset the same algorithm just with a different label <lab>. This allows you to reference the individual algorithms within the actual figure. Of course, you could change this to just represent (a), (b), (c) and (d).

geometry provided a3paper and landscape orientation, since the algorithms were just examples (which were large). You might not require this in your regular usage. The same goes for the choice of .4\linewidth in your minipage. You could adjust this, depending on your eventual output. minipage also allows for an option argument specifying the vertical alignment. So, for example, if Algorithm2 is shorter than Algorithm 1, you could specify [t] for the second so their are vertically aligned at the top.

-
Thanks, works great so far. What would I need to do to get "(a) caption" instead of "Algorithm 1: caption"? Moreover, when I reference an algorithm using \ref{algorithmA}, I want the reference to print the figure number, and then the letter indicating the algorithm within the figure, e.g. "see Figure 1a". –  Hermann Jan 7 '12 at 20:49
@Hermann: see my answer –  cmhughes Jan 7 '12 at 20:56