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}
The relevant internal commands in algorithm2e
are \algocf@uIf
, \algocf@uElseIf
and \algocf@Else
. The following modifications of these commands insert your extra formatting:
\documentclass[10pt,a4paper]{article}
\usepackage[vlined]{algorithm2e}
\newcommand\BlockIf[1]{\KwSty{Start If} \\ #1 \\ \KwSty{End If}}
\newcommand\BlockElseIf[1]{\KwSty{Start Else If} \\ #1 \\ \KwSty{End Else If}}
\newcommand\BlockElse[1]{\KwSty{Start Else} \\ #1 \\ \KwSty{End Else}}
\makeatletter
\renewcommand{\algocf@uIf}[2]{\If@ifthen{#1}\If@noend{\BlockIf{#2}}}
\renewcommand{\algocf@uElseIf}[2]{\ElseIf@elseif{#1}\If@noend{\BlockElseIf{#2}}}
\renewcommand{\algocf@Else}[1]{\Else@else\If@endif{\BlockElse{#1}}}
\makeatother
\begin{document}
\begin{algorithm}[H]
\uIf{1}{if...}
\uElseIf{2}{else if...}
\Else{else...}
\end{algorithm}
\end{document}
The set-up for the display of if-then-else structures is dealt with by the command \SetKwIF
which in standard form has 8 arguments, the first three of which are If
ElseIf
and Else
. It uses these names to build the commands modified above and a number of their variants e.g. for dealing with optional arguments ()
for adding comments. You will need to redefine these variant commands too if you need that facility.
Best Answer
If you only want what you show, then you can adjust
\algomargin
:The second algorithm is merely for reference of the alignment.