I tried this example Place four algorithms (algorithm2e) in a subfigure and arrange them in two rows and two columns on two column documents. It doesn't work. The error message is
! LaTeX Error: [H] in two columns mode is not allowed for algorithms.
Is there anyway to go?
The code example is:
\documentclass[twocolumn]{article}
\usepackage{algorithm2e}
\usepackage{caption}
\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}{
read current\;
\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}
Best Answer
algorithm2e
provides its own[H]
float parameter, which sidesteps setting its contents as a float. However, intwocolumn
mode, it produces an error. I'm not sure why. You can avoid this by setting\@latex@error
to\@gobble
temporarily. Using your example, I've added a\removelatexerror
macro inside a\begingroup
...\endgroup
. More specifically, the definitionis placed inside a group, inside
\myalgorithm
:Also, since you're spreading the 4 algorithms across both columns, it's best to use
figure*
rather thanfigure
:Obviously you won't be using
\myalgorithm
in your actual document. You could just place\removelatexerror
inside each of thesubfigure
environments.