I will to align comments in another column, package algorithm.
(Similar question: Faulty right alignment for comments in algorithmic package), but I will to become that result:
\usepackage{algorithm,algorithmic}
\renewcommand{\algorithmiccomment}[1]{\bgroup\hfill//~#1\egroup}
[...]
\begin{algorithm}
\caption{$function(n : \mathbb{N}_0) : \mathbb{N}_0$}
\label{algo1}
\begin{algorithmic}[1]
\REQUIRE $n \in \mathbb{N}_0$
\STATE $result \leftarrow 0 : \mathbb{N}_0$
\STATE $temp \leftarrow 1 : \mathbb{N}_0$
\FOR[$n$ Durchläufe]{$i \leftarrow 0$ \TO $n-1$}
\FOR{$j \leftarrow i$ \TO $i$}
\STATE $temp \leftarrow temp \cdot 2$ \COMMENT{Multiplikation}
\ENDFOR
\STATE $result \leftarrow result + temp$ \COMMENT{Addition}
\STATE $temp \leftarrow 1$
\ENDFOR
\RETURN $result$
\end{algorithmic}
\end{algorithm}
Best Answer
Here is an
algorithmicx
implementation:\COMMENT[<len>]{<stuff>}
is reformatted to put the comment<stuff>
in a box of width<len>
(default is.5\linewidth
) flush right. You can adjust this to your liking.A pure
algorithmic
implementation is somewhat different:It uses e-TeX (for the string comparison), but that can be changed. The reason for the difference in implementation is mainly because of the way loops are handled in
algorithmic
- the comment is inserted as an optional argument to the loop structure. As such, it's difficult to allow another optional argument for the length parameter as above in thealgorithmicx
implementation. The work-around is to define a length\commentindent
that handles this in a similar way the optional argument would handle things.Here's a full implementation, with the same output: