[Tex/LaTex] Algorithm2e comment line alignment problem

algorithm2ecommentshorizontal alignment

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}

Best Answer

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