# algorithm2e with 'ruled' but with caption underneath?

I would like to present an algorithm in my article. For that I am using the algorithm2e package (and I have used it for many years).

I want to use the ruled option, i.e., I load the package like this:

\usepackage[ruled,noend,noline,slide]{algorithm2e}


Unfortunately, with the ruled option, the algorithm2e package puts the caption above the pseudo code.

However, I want the caption to appear underneath the pseudo-code, just like any other caption in figures or tables.

Does anybody know, what I need to change in the style file to make this possible? Or can I achieve it with the help of the float package? (Note that I still would like to have a kind of algorithmic environment like this

\begin{myalgorithm}[tb]
\If{...}
\caption{An algorithm}
\end{myalgorithm}


) I am using the latest version, i.e., 4.01, of algorithm2e.sty.

-
Thanks for asking the question. In my document, the default setting put the caption below the algorithm which I didn;t like. After seeing this post i realized that using the 'ruled' option would put the caption above the algorithm. –  A. K. Aug 5 '12 at 18:31

You can achieve this by defining a new environment which puts the caption under instead of the default top, and adjusting the vertical skip between a rule and the caption text:

\documentclass{book}
\usepackage[ruled,noend,noline,slide]{algorithm2e}

\makeatletter
\newenvironment{Ualgorithm}[1][htpb]
{\def\@algocf@post@ruled{\kern\interspacealgoruled\hrule  height\algoheightrule\kern3pt\relax}%
\def\@algocf@capt@ruled{under}
\begin{algorithm}[#1]}
{\end{algorithm}}
\makeatother

\begin{document}

\begin{algorithm}
\If{$i<5$}{some action}
\caption{A ruled algorithm with caption above}
\end{algorithm}

\begin{Ualgorithm}
\If{$i<5$}{some action}
\caption{A ruled algorithm with caption below}
\end{Ualgorithm}

\end{document}


-
@Gabriel: That should be easy to fix: Modify \newenvironment{Ualgorithm}[1][h]...\begin{algorithm}[#1]. This will make the default float placement h, unless otherwise specified using (say) \begin{Ualgorithm}[tb]...\end{Ualgorithm}. –  Werner Sep 6 '11 at 17:28
@Werner: I added the optional argument; however, some tests I did, showed that it was not really necessary. The environment, as it was, behaved like a float. Perhaps I didn't do enough tests...? –  Gonzalo Medina Sep 6 '11 at 17:38
algorithm2e.sty defines the algocf[*] environment as \newenvironment{algocf}{\@float{algocf}}{\end@float} which may have a default that doesn't correspond to the OP's taste. The testing is subjective, of course, since you don't have a document resembling the OP's setup. –  Werner Sep 6 '11 at 17:45
LaTeX issues a warning if the optional parameter to Ualgorithm is empty, substituting it to the default htbp. Perhaps specify this as the default [htbp] to avoid this warning. –  Werner Sep 6 '11 at 17:48
@Werner: yes, but what I meant (perhaps I wasn't clear) is that the Ualgorithm environment (as it was initially defined without the optional argument) would receive (and obey) the optional argument for placement specifiers. –  Gonzalo Medina Sep 6 '11 at 17:48