[Tex/LaTex] How to use the algorithm2e package with IEEEtran class

algorithm2ealgorithmsfloatsieeetran

The IEEEtran documentation suggests that I should not use the floating algorithm environment of algorithm2e.sty.

B. Algorithms

IEEE publications use the figure environment to contain
algorithms that are not to be a part of the main text flow. Peter
Williams’ and Rogerio Brito’s algorithmic.sty package or Szász
János’ algorithmicx.sty package (the latter is designed to be
more customizable than the former) may be of help in producing
algorithm-like structures (although authors are of course free to use
whatever LaTeX commands they are most comfortable with in this
regard). However, do not use the floating algorithm environment of
algorithm.sty (also by Williams and Brito) or algorithm2e.sty (by
Christophe Fiorio) as the only floating structures IEEE uses are
figures and tables.
Furthermore, IEEEtran will not be in control of
the (non-IEEE) caption style produced by the algorithm.sty or
algorithm2e.sty float environments.

I tried using the suggested packages algorithmic and algorithmicx but I am not satisfied with the results and I would rather use the algorithm2e package.

A colleague told me that the suggestion of IEEE applies only to the floating algorithm environment of the package and not to the package itself. She suggested that I wrap the algorithm in a figure environment.

I tried to simply wrap the entire algorithm in a figure:

\documentclass[journal, a4paper]{IEEEtran}

\usepackage[ruled,norelsize]{algorithm2e}

\begin{document}

%\begin{figure}[!t]

\begin{algorithm}[h]
 \caption{multiobjective DE} 
 initialize population $P = \left \{ X_{1}, ... , X_{N} \right \} $\;
 \For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
 {
    Do things \;
    Trim the population to size $N$ using nondominated sorting and diversity estimation \;
 }
\end{algorithm}

%\end{figure}

\end{document}

But I get a

Not in outer par mode. \begin{algorithm}[h]

error. Obviously I cannot do it like this. How can I fix this?

Best Answer

You can follow this way.

First of all, you need to use the H specifier so to force the algorithm environment not to float.

Then, since the H specifier is not allowed in two-column mode, you can use the workaround described by Werner in this answer, that is add the following lines in the preamble

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

and issue the command \removelatexerror inside the figure environment.

MWE:

\documentclass[journal, a4paper]{IEEEtran}

\usepackage[ruled,norelsize]{algorithm2e}

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

\begin{document}

\begin{figure}[!t]
 \removelatexerror
  \begin{algorithm}[H]
   \caption{multiobjective DE}
   initialize population $P = \left \{ X_{1}, ... , X_{N} \right \} $\;
   \For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
   {
      Do things \;
      Trim the population to size $N$ using nondominated sorting and diversity estimation \;
   }
  \end{algorithm}
\end{figure}

\end{document} 

Output:

enter image description here

If you want, you can now even add a \caption to the figure, e.g.

\begin{figure}[!t]
 \caption{My algorithm}
 \removelatexerror
 ...

yields

enter image description here