5

I'm using IEEEtran with \usepackage[boxed,linesnumbered]{algorithm2e}. I have one pseudocode/algorithm. My caption inside the algorithm environment appears as "Algorithm 1". Since I only have one algorithm, I think I should be using the figure caption instead. What do you think and how can I force my caption to appear as a figure caption?

To provide information needed for the comments here is a snippet of my code:

\IncMargin{1em}
\begin{algorithm}
\DontPrintSemicolon
\Indm
\KwInputs{\hspace*{0.3em}sth}
\KwOutput{sth}
\Indp
this is the body of my algo.
\caption{this is my caption.}
\end{algorithm}
\DecMargin{1em}

1 Answer 1

3

The simplest solution is to use the figure option for algorithm2e; in this way, algorithms are put in figure environments and so are numbered as figures and included in the \listoffigures:

\documentclass{IEEEtran}
\usepackage[figure]{algorithm2e} 

\begin{document}

\begin{algorithm}
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e}
\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\;
  }
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}

An image of the result:

enter image description here

Update

According to comments to the original answer, algorithm2e is loaded with options boxed and linesnumbered; in this case, adding the figure option, as in

\usepackage[boxed,linesnumbered,figure]{algorithm2e}

has the undesired effect that the caption is moved inside the frame boxing the algorithm. To prevent this, some additional work is needed, but since it is just for one algorithm, I think the effort is worthy.

The idea is to load the package only with the figure option

\usepackage[figure]{algorithm2e}

and then manually box and frame the algorithm also using \LinesNumbered to get the numbers:

\documentclass{IEEEtran}
\usepackage[figure]{algorithm2e} 

\newsavebox\mybox

\begin{document}

Algo de texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba
\begin{algorithm}
\LinesNumbered
\DontPrintSemicolon
\begin{lrbox}{\mybox}
\begin{minipage}{\hsize}
% Write your algorithm starting here
\Indentp{-1em}
\KwIn{something}
\KwOut{something}
\Indentp{1.2em}
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e}
\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 your algorithm here
\end{minipage}%
\end{lrbox}
\hspace*{-10pt}\framebox[\columnwidth]{\hspace*{15pt}\usebox\mybox\par}
\caption{How to make algorithms}
\end{algorithm}

\end{document}

The result:

enter image description here

11
  • I used \usepackage[boxed,linesnumbered,figure]{algorithm2e} but the caption moved inside the box. Can I move it out?
    – Elnaz
    Nov 30, 2014 at 3:54
  • @Elnaz you can add figure as in \usepackage[boxed,linesnumbered,figure]{algorithm2e}. Nov 30, 2014 at 3:58
  • I used \usepackage[boxed,linesnumbered,figure]{algorithm2e} but the caption moved inside the box. Can I move it out?
    – Elnaz
    Nov 30, 2014 at 4:10
  • @Elnaz Please see my updated answer. Nov 30, 2014 at 4:50
  • I tried to increase the width of the box by changing {30pt} in your code but I can't get it centered. I want the column width.
    – Elnaz
    Nov 30, 2014 at 5:00

You must log in to answer this question.

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