1

I am using Windows 7 and getting a LaTeX Error in an algorithm; I am new to LaTeX and already spent a lot of time to figure this out but no luck. Could you please help me here?

\begin{algorithm}
\caption{Calculate Tree Labelling.}
\label{alg:treeLabel}
\begin{algorithmic}[1]

\While{ S is not empty}{
Pop out top vertex from S. Let v=S.pop().\\
}
 \end{algorithmic}
\end{algorithm}

Latex Error:

 Something's wrong -- perhaps a missing \item. 
         Pop out top vertex from S. Let v=S.pop()
        ! Undefined control sequence.
        \while
              {S is not empty}{   
        Something's wrong -- perhaps a missing \item. 
         \end{algorithm}.

Please help here.

  • 1
    Your code snippet has \While, while your error message says \while; are these the same, or is one a typo, or..? (Note that *TeX is case-sensitive when it comes to things, unlike Windows, which does not always distinguish between upper- and lowercase words.) – jon Sep 3 '15 at 1:43
2

enter image description here

\documentclass[a4paper,10pt]{report}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}

\begin{algorithm}
\caption{Calculate Tree Labelling.}
\label{alg:treeLabel}
\begin{algorithmic}[1]
\While {S is not empty}
\State {Pop out top vertex from S. Let v=S.pop().}
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document} 

When using \While, you should close with \EndWhile, that is the main cause of the error. Besides, the statement Pop out top vertex from S. Let v=S.pop(). should be put after a \State command to get it correctly indented and no need for \\s.

More examples with ELSE IF

\documentclass[a4paper,10pt]{report}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}

\begin{algorithm}
\caption{Calculate Tree Labelling.}
\label{alg:treeLabel}
\begin{algorithmic}[1]
\While {S is not empty}
\State {Pop out top vertex from S. Let v=S.pop().}
\EndWhile

\If {Expression 1} 
  \If {Expression 2}
      \State Statement 1.
      \State Statement 2. 
  \EndIf
  \ElsIf {Expression 3}
      \State  Statement 3.
      \State  Statement 4.
  \Else
      \State  Statement 4.
\EndIf

\end{algorithmic}
\end{algorithm}
\end{document} 

enter image description here

Note how we write \ElsIf and not \ElseIf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.