[Tex/LaTex] Place comment after while statement in algorithmic

algorithmiccommentsformatting

I am having some trouble placing plain text in my algorithm using the algorithmic package. The problem only seems to occur after a '\WHILE' command. I need to start my 'while' loop, then immediately place a piece of text with a brief description of the next sub-section of code.

I currently insert text in the code as follows:

\documentclass{article}
\usepackage{algorithmic}

\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}

\STATE line 4

\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}

Which produce the type of text I want within the code, i.e. a piece of text with no algorithm line number associated with it.

So, based on that, what I think should work is:

\documentclass{article}
\usepackage{algorithmic}

\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}

\textit {describe the next step 1}

\STATE line 4

\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}

However, this produces errors:

line 12: Something's wrong--perhaps a missing \item. \STATE

line 12: Something's wrong--perhaps a missing \item. \STATE

..snip..

line 16: Something's wrong--perhaps a missing \item. \ENDWHILE

and nonsense formatting on the output.

The nearest I can get is to insert an additional line (which I do not want) as follows:

\documentclass{article}
\usepackage{algorithmic}

\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}
\STATE Inserted Text

\textit {describe the next step 1}

\STATE line 4

\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}

or use the \STATE command on the line with the text, which gives the text a line number.

Any ideas what's going on and, of course, what the cure is?

After the answer from Werner (thanks) I thought this might help clarify it a bit better. The pseudo-code is heavily mathematical so not immediately clear what's happening. So I want to place a one line comment 'describe lines 4-6' this may be clearer:

\documentclass{article}
\usepackage{algorithmic}

\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}

\STATE line 4
\STATE line 5
\STATE line 6

\textit {describe lines 7-9}
\STATE line 7
\STATE line 8
\STATE line 9

\textit {describe lines 10-12}
\STATE line 10
\STATE line 11
\STATE line 12
\ENDWHILE
\end{algorithmic}
\end{document}

Best Answer

Under algorithmic you can add a comment on the same line as your \WHILE using an optional argument:

enter image description here

\documentclass{article}

\usepackage{algorithmic}

%\newcommand{\algorithmiccomment}[1]{\{#1\}}% Original definition
\renewcommand{\algorithmiccomment}[1]{\textit{#1}}% Updated definition

\begin{document}

\begin{algorithmic}[1]
  \STATE line 1
  \STATE line 2
  \WHILE [describe while condition] {test}
    \STATE line 4
    \textit {describe the next step 2}
    \STATE line 5
  \ENDWHILE
\end{algorithmic}

\end{document}

Note that the default formatting for comments is via \algorithmiccomment{<comment>} which sets {<comment>} - a comment inside a pair of curly braces {...}. Above I've reformatted that to \textit{<comment>}.


The following minimal example builds on what has already been suggested above by providing a starred version of \STATE*. The starred version doesn't print any line number:

enter image description here

\documentclass{article}

\usepackage{algorithmic,xpatch}

%\newcommand{\algorithmiccomment}[1]{\{#1\}}% Original definition
\renewcommand{\algorithmiccomment}[1]{\textit{#1}}% Updated definition

\makeatletter

\xpatchcmd{\algorithmic}% <cmd>
  {\newcommand{\STATE}{\ALC@it}}% <search>
  {\newcommand{\STATE}{\@ifstar\STATEstar\STATEnostar}}% <replace>
  {}{}% <success><failure>
\newcommand{\STATEstar}{\item[]}
\newcommand{\STATEnostar}{\ALC@it}
\makeatother

\begin{document}

\begin{algorithmic}[1]
  \STATE line 1
  \STATE line 2
  \WHILE [describe while condition] {test} \label{first}
    \STATE* \textit {describe the item on line~\ref{first}}
    \STATE line 4 \label{second}
    \textit {describe step~\ref{second}}
    \STATE line 5
  \ENDWHILE
\end{algorithmic}

\end{document}
Related Question