# Revtex4-1 and algorithm2e indentation clash

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?

-

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}


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}


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}

-
 Thanks Gonzalo, that fixed the indentation problem. Could you try compiling using the following document class: \documentclass{revtex4-1}. On my system, the document is rendered properly but I get the following errors when doing so:! Missing \endcsname... ! Missing number, treated as zero... ! Extra \endcsname... – raspoutine Sep 5 '12 at 18:07 @raspoutine Hmmm. You're right, Using revtex4-1 produces an error, this time due to the algorithm package. Could using another package be an option for you? (algorithm only provides the rules and the caption, but that can be produced with other packages). – Gonzalo Medina Sep 5 '12 at 18:16 @raspoutine I updated my answer with two possible solutions. – Gonzalo Medina Sep 5 '12 at 19:04 Yes, using another package is an option for me. I'm not sticked to any alg* package, I just fancy the nice vertical lines and the line numbering provided by some packages. But the revtex4-1 is not an option, as it is required by my publisher. – raspoutine Sep 5 '12 at 21:15 @raspoutine then perhaps one of the options I gave in my updated answer is useful for you? (Unfortunately, vertical lines are not easy to achieve now). – Gonzalo Medina Sep 5 '12 at 21:45