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}
I have re-listed the original non-automated approach here, since the automated approach (below) was shown to exhibit deficiencies. With the manual approach, the comment is placed in a \parbox
, but the user must specify the width of it. The underfull boxes cited by the OP are remedied by making the comment explicitly \raggedright
.
\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[tbp]
\LinesNumbered
\caption{Text}
\label{alg:Algo}
\BlankLine
$a \gets (a+1)^a$\tcp*{\parbox[t]{3.3in}{\raggedright One long comment line which should
be aligned in such a way that the comment shouldn't go into the algorithm}}
\Return{Result}\;
\end{algorithm}
\end{document}
-----AUTOMATED APPROACH THAT FAILS WHEN NESTED IN A LOOP
You can do it inside a \parbox
. EDITED to automate the width calculation of the \parbox
. Here, I introduce \algolines{}{}
which takes the \tcp
argument as its first argument and the multiline comment as its second.
\documentclass{article}
\usepackage{algorithm2e}
\newlength\algowd
\def\savewd#1{\setbox0=\hbox{#1\hspace{.7in}}\algowd=\wd0\relax#1}
\newcommand\algolines[2]{\savewd{#1}%
\tcp*{\parbox[t]{\dimexpr\algowidth-\algowd}{#2}}}
\begin{document}
\begin{algorithm}[tbp]
\LinesNumbered
\caption{Text}
\label{alg:Algo}
\BlankLine
\algolines{$a \gets (a+1)^a$}{One long comment line which should be aligned in such a way that the comment shouldn't go into the algorithm}
\Return{Result}\;
\end{algorithm}
\begin{algorithm}[tbp]
\LinesNumbered
\caption{Text}
\label{alg:Algo}
\BlankLine
\algolines{$a \gets (a+1)^a (b+1)^b$}{One long comment line which should be aligned in such a way that the comment shouldn't go into the algorithm}
\Return{Result}\;
\end{algorithm}
\end{document}
Best Answer
You can also use
\DontPrintSemicolon
.