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}
When you load algorithm2e
with linesnumbered
, it executes \nl
at every paragraph break (taken from algorithm2e.sty
):
%
% line numbering
%
\newcommand{\LinesNumbered}{%
\setboolean{algocf@linesnumbered}{true}%
\renewcommand{\algocf@linesnumbered}{\everypar={\nl}}}%
So, \nl
is the thing we are interested in adjusting.
You can remove the line number for a single line using the following \nonl
command:
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
Here's a minimal example showing the output:
\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
\makeatother
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
\nosemic this is a very long statement that has to be\;
\pushline\dosemic\nonl wrapped over two lines\;
\popline current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
\nonl
redefines \nl
for one usage. That usage is just to redefine itself to use the original form (a similar-style trick is used in Cunning (La)TeX tricks).
Best Answer
It appears to be easier to change it for Case than Otherwise, but this seems to work: