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