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}
