# How to align comments in algorithm code?

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}

-
Please always supply a complete (\documentclass to \end{document}) example document that demonstrates the problem and allows people to test their answers. – David Carlisle May 24 '14 at 22:19

Here is an algorithmicx implementation:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm,algcompatible,amssymb,amsmath}
\renewcommand{\COMMENT}[2][.5\linewidth]{%
\leavevmode\hfill\makebox[#1][l]{//~#2}}
\algnewcommand\algorithmicto{\textbf{to}}
\algnewcommand\RETURN{\State \textbf{return} }
\begin{document}

\begin{algorithm}
\caption{$\text{function}(n : \mathbb{N}_0) : \mathbb{N}_0$}\label{algo1}
\begin{algorithmic}[1]
\REQUIRE $n \in \mathbb{N}_0$
\STATE $\text{result} \leftarrow 0 : \mathbb{N}_0$
\STATE $\text{temp} \leftarrow 1 : \mathbb{N}_0$
\FOR{$i \leftarrow 0$ \algorithmicto{} $n-1$} \COMMENT{$n$ Durchläufe}
\FOR{$j \leftarrow i$ \algorithmicto{} $i$}
\STATE $\text{temp} \leftarrow \text{temp} \cdot 2$ \COMMENT{Multiplikation}
\ENDFOR
\STATE $\text{result} \leftarrow \text{result} + \text{temp}$ \COMMENT{Addition}
\STATE $\text{temp} \leftarrow 1$
\ENDFOR
\RETURN $\text{result}$
\end{algorithmic}
\end{algorithm}
\end{document}


\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:

\usepackage{letltxmacro}

\newlength{\commentindent}
\setlength{\commentindent}{.5\textwidth}
\makeatletter
\renewcommand{\algorithmiccomment}[1]{\unskip\hfill\makebox[\commentindent][l]{//~#1}\par}
\LetLtxMacro{\oldalgorithmic}{\algorithmic}
\renewcommand{\algorithmic}[1][0]{%
\oldalgorithmic[#1]%
\renewcommand{\ALC@com}[1]{%
\ifnum\pdfstrcmp{##1}{default}=0\else\algorithmiccomment{##1}\fi}%
}
\makeatother


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 the algorithmicx 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:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm,algorithmic,amssymb,amsmath,letltxmacro}
\newlength{\commentindent}
\setlength{\commentindent}{.5\textwidth}
\makeatletter
\renewcommand{\algorithmiccomment}[1]{\unskip\hfill\makebox[\commentindent][l]{//~#1}\par}
\LetLtxMacro{\oldalgorithmic}{\algorithmic}
\renewcommand{\algorithmic}[1][0]{%
\oldalgorithmic[#1]%
\renewcommand{\ALC@com}[1]{%
\ifnum\pdfstrcmp{##1}{default}=0\else\algorithmiccomment{##1}\fi}%
}
\makeatother

\begin{document}

\begin{algorithm}
\caption{$\text{function}(n : \mathbb{N}_0) : \mathbb{N}_0$}\label{algo1}
\begin{algorithmic}[1]
\REQUIRE $n \in \mathbb{N}_0$
\STATE $\text{result} \leftarrow 0 : \mathbb{N}_0$
\STATE $\text{temp} \leftarrow 1 : \mathbb{N}_0$
\FOR[$n$ Durchläufe]{$i \leftarrow 0$ \TO $n-1$}
\FOR{$j \leftarrow i$ \TO $i$}
\STATE $\text{temp} \leftarrow \text{temp} \cdot 2$ \COMMENT{Multiplikation}
\ENDFOR
\STATE $\text{result} \leftarrow \text{result} + \text{temp}$ \COMMENT{Addition}
\STATE $\text{temp} \leftarrow 1$
\ENDFOR
\RETURN $\text{result}$
\end{algorithmic}
\end{algorithm}
\end{document}

-