[Tex/LaTex] Revtex4-1 and algorithm2e indentation clash

algorithm2erevtex

there seems to exists a clash between revtex4-1 and the algorithm2e package. Whenever I try to insert an algorithm block when using the revtex4-1 document class, the tabulation is not working properly. The following latex code should hopefully be self explanatory.

%\documentclass[10]{article}
\documentclass[10]{revtex4-1}

% these are to avoid another clash between revtex and algorithm2e
\makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
% needed includes
\usepackage[ruled]{algorithm2e}
\usepackage{algorithmic}

\begin{document}

The following pseudocode exibits a clash between revtex4-1 and algorithm2e.

\begin{algorithm}
\caption{Calculate something}
\begin{algorithmic}
\FOR{every thing you}
    \FOR{every step you}
        \STATE{compute something}
    \ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}

\end{document}

This is a how the rendered pdf shows on my system (sorry, I'm not allowed to insert images in this post). The tabulation is correct when using the simple article document class. I am using Texmaker (with texLive) under Ubuntu 10.04.

Using the revtex4-1 document class:

for every thing you do
for every step you do
compute something
end for
end for

Using the article document class:

for every thing you do
    for every step you do
        compute something
    end for
end for

Anybody with a solution to make these two work together?

Best Answer

There is an incompatibility between the algorithm2e/algorithmic packages and revtex4-1; one possibility is to use algcompatible instead of algorithmic (the syntax of the commands is the same and this solves the indentation problem) and to use the newfloat package to define a new algorithm float. Here's an example illustrating this approach (I used a table and a figure environments to show how the newly defined float behaves consistently with the class):

\documentclass{revtex4-1}
\usepackage{algcompatible}
\usepackage{newfloat}

\DeclareFloatingEnvironment[
    fileext=loa,
    listname=List of Algorithms,
    name=ALGORITHM,
    placement=tbhp,
]{algorithm}

\begin{document}

\begin{figure}
\rule{2cm}{2cm}
\caption{A test caption for a figure}
\end{figure}

\begin{table}
\rule{2cm}{2cm}
\caption{A test caption for a table}
\end{table}

\begin{algorithm}
\begin{algorithmic}
\FOR{every thing you}
    \FOR{every step you}
        \STATE{compute something}
    \ENDFOR
\ENDFOR
\end{algorithmic}
\caption{Calculate something}
\end{algorithm}

\end{document}

enter image description here

If one wants to keep the ruled style of the algorithm environmet, as defined in algorithm2e, some additional work has to be done; in this case, the caption package could be used (providing one makes the necessary adjustments to recover the formatting defined in revtex4-1); here's such possibility with a little example:

\documentclass{revtex4-1}
\usepackage{newfloat,algcompatible}
\usepackage[size=small]{caption}
\usepackage{etoolbox}

\AtBeginEnvironment{algorithm}{\noindent\hrulefill\par\nobreak\vskip-5pt}
\usepackage{newfloat}

\DeclareFloatingEnvironment[
    fileext=loa,
    listname=List of Algorithms,
    name=ALGORITHM,
    placement=tbhp,
]{algorithm}
\DeclareCaptionFormat{algorithms}{\vskip-15pt\hrulefill\par#1#2#3\vskip-6pt\hrulefill}
\captionsetup[algorithm]{singlelinecheck=off,format=algorithms}

\begin{document}

\begin{figure}
\rule{2cm}{2cm}
\caption{A test caption for a figure}
\end{figure}

\begin{algorithm}
\begin{algorithmic}
\FOR{every thing you}
    \FOR{every step you}
        \STATE{compute something}
    \ENDFOR
\ENDFOR
\end{algorithmic}
\caption{Calculate something}
\end{algorithm}

\end{document}

enter image description here

If, for some reason, the newfloat packageis not available, one can use the trivfloat instead:

\documentclass{revtex4-1}
\usepackage{algcompatible}
\usepackage[floatrow]{trivfloat}

\trivfloat{algorithm}
\renewcommand\algorithmname{ALGORITHM}

\begin{document}

\begin{figure}
\rule{2cm}{2cm}
\caption{A test caption for a figure}
\end{figure}

\begin{table}
\rule{2cm}{2cm}
\caption{A test caption for a table}
\end{table}

\begin{algorithm}
\begin{algorithmic}
\FOR{every thing you}
    \FOR{every step you}
        \STATE{compute something}
    \ENDFOR
\ENDFOR
\end{algorithmic}
\caption{Calculate something}
\end{algorithm}

\end{document}
Related Question