17

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?

1 Answer 1

16

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

5
  • 1
    Ideally one would like to redefine algorithm to do all of this error removal and figure placement automatically, so you can just use \begin{algorithm}[t]...\end{algorithm}.
    – Werner
    Commented Nov 29, 2013 at 20:25
  • 1
    @Werner As it is, one can add a \caption to the figure, too. Commented Nov 29, 2013 at 20:40
  • @karlkoeller Perfect! Exactly what I needed Commented Nov 29, 2013 at 21:25
  • @MartinDrozdik Glat it helped. Commented Nov 29, 2013 at 21:25
  • 1
    This works greatly and preserves the standard algorithm style, great! Thank's a lot! Also, note that to have the caption below the algorithm, the \caption{...} command must be placed at the bottom, near \end{figure}.
    – gaborous
    Commented Jan 5, 2015 at 23:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .