2

I would like to align a comment within the algorithm environment of algorithm2e in such a way that each line of a multi line comment is left aligned.

See the following MWE. In this MWE the second and third line of the comment are left aligned with the pseudo code of the algorithm. I would like to align them with the first line of the comment, so the word 'in' and 'algorithm' should be aligned to the word 'One' of the first line.

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[tbp]
\LinesNumbered
\caption{Text}
\label{alg:Algo}

\BlankLine

$a \gets (a+1)^a$\tcp*{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}

Edit: An edit in response to the answer of Steven B. Segletes. There is a problem with the alignment if the line with the comment is nested in a loop. Also there are warnings of undefull hboxes, because of the parbox.

MWE:

\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

\ForEach{$a \in A$}{
   \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}

\end{document}
2

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}

enter image description here

  • Thank you for the solution it works for my MWE. But now I have a problem if the line with the comment is nested in e.g. a foreach the comment is always put on a second line under the first line with the pseudo code. Do you have an idea for this case? – user2653422 Aug 6 '14 at 11:37
  • I extended my initial question. There seems to be also a problem with underfull hboxes – user2653422 Aug 6 '14 at 11:45
  • @user2653422 I took the width of the algorithm (plus a little) and used that to calculate the comment width. I have no way of knowing what additional nesting elements etc., are taking up the line width. Thus, it is clear my automated approach will not work. Shall I delete the answer? At a minimum, I will relist the original (non-automated approach). – Steven B. Segletes Aug 6 '14 at 12:03
  • Thank you, I'm happy with the manual way (and the solution for the underfull hboxes) as multi line comments don't appear very often in my document. – user2653422 Aug 6 '14 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.