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 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}
share|improve this question

2 Answers 2

up vote 6 down vote accepted

algorithm2e provides its own [H] float parameter, which sidesteps setting its contents as a float. However, in twocolumn 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 definition

\makeatletter
\newcommand{\removelatexerror}{\let\@latex@error\@gobble}
\makeatother

is placed inside a group, inside \myalgorithm:

\newcommand{\myalgorithm}{%
\begingroup
\removelatexerror% Nullify \@latex@error
\begin{algorithm*}[H]
%...
\end{algorithm*}
\endgroup}

Also, since you're spreading the 4 algorithms across both columns, it's best to use figure* rather than figure:

enter image description here

Obviously you won't be using \myalgorithm in your actual document. You could just place \removelatexerror inside each of the subfigure environments.

share|improve this answer

I had similar issues...

I simply removed [H] from the code as below.

\begin{algorithm*}[H]

to

\begin{algorithm*}

Then the issue solved.

share|improve this answer
2  
Welcome to TeX.SX! –  mafp Sep 8 '13 at 19:49

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.