# Why does the placement of a \COMMENT in the algorithmic enviroment matter?

I'm using the algorithmic package. When I start off a FORALL with a COMMENT, LaTeX behaves as though I've created an invalid loop body. If I put it as the second line, with a STATE first, all is well.

Demo:

\documentclass[a4paper,10pt]{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\FORALL{ $a \in B$}
\STATE X
\COMMENT{Blah.}
\ENDFOR
\end{algorithmic}
\end{document}


If you reverse the order of the STATE and COMMENT, this won't compile.

The error messages are 4 "Something's wrong--perhaps a missing \item..."
3 for the \STATE, and 1 for the \ENDFOR.


I have two questions:

1. Why does this happen?
2. How can I put a COMMENT as the first item in a loop?

EDIT: So I notice that there is a way to put a comment immediately after the \FORALL as

\FORALL[comment]{loop condition}


but I actually want to put the comment right above the first statement in the loop -- it's not about the loop condition.

I can do this with a \vspace tag, but that leaves the opening bracket of the comment, i.e. the '{' next to the loop with the body below, i.e.

\FORALL[\vspace{0.5cm} comment]{loop condition}


As suggested below, it is possible to solve that problem using the statement

\FORALL{loop condition}
\STATE \COMMENT{blah}
\STATE X
\ENDFOR


but the resulting comment will now have a line number of its own in a numbered algorithmic environment.

-

You need to "fool" the algorithmic environment into posting just a comment on a line via \STATE \COMMENT{Blah}. The reason for this (in answer to your first question) is because the algorithmic environment handles it's contents like a list (similar to \begin{enumerate}...\end{enumerate} or \begin{itemize}...\end{itemize}). And, in a similar context, it requires a command like \item to move to the next item, or even start the list. Such a command is \STATE. That, with the following minimal example should answer both questions:

\documentclass[a4paper,10pt]{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\FORALL{$a \in B$}
\STATE \COMMENT{Blah.} % single line comment
\STATE X
\COMMENT{Blah.}
\ENDFOR
\end{algorithmic}
\end{document}​


EDIT: The above only works if you do not use line numbers. If you do use line numbers (via \begin{algorithmic}[1]), then you can define a new macro \LCOMMENT{<comment>} (short for "Line COMMENT") that does the trick:

\documentclass[a4paper,10pt]{article}
\usepackage{algorithmic}
\makeatletter
\newcounter{ALC@tempcntr}% Temporary counter for storage
\newcommand{\LCOMMENT}[1]{%
\setcounter{ALC@tempcntr}{\arabic{ALC@rem}}% Store old counter
\setcounter{ALC@rem}{1}% To avoid printing line number
\item \{#1\}% Display comment + does not increment list item counter
\setcounter{ALC@rem}{\arabic{ALC@tempcntr}}% Restore old counter
}%
\makeatother
\begin{document}
\begin{algorithmic}[1]
\FORALL{ $a \in B$}
\LCOMMENT{Blah.} % single line comment
\STATE X
\COMMENT{Blah.}
\ENDFOR
\end{algorithmic}
\end{document}​


There may be other/better/more elegant solutions as well.

-
This is good, but still not quite correct. The resulting comment is given a line number, where other comments are not. – John Doucette Jul 20 '11 at 17:51
I've updated my answer to include both options - when having an algorithmic environment with and without line numbers. – Werner Jul 20 '11 at 18:52
Thanks, this works great. – John Doucette Jul 20 '11 at 19:17