未定义的控制序列和某些东西丢失,可能是缺少 \item

未定义的控制序列和某些东西丢失,可能是缺少 \item

我正在使用 Windows 7,在算法中遇到了 LaTeX 错误;我是 LaTeX 新手,已经花了很多时间解决这个问题,但没有成功。你能帮我吗?

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

乳胶错误:

 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}.

请帮忙。

答案1

在此处输入图片描述

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

使用 时\While,应以 结尾\EndWhile,这是导致错误的主要原因。此外,语句Pop out top vertex from S. Let v=S.pop().应放在\State命令之后,以使其正确缩进,无需\\s。

更多 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} 

在此处输入图片描述

请注意我们如何写\ElsIf而不是\ElseIf

相关内容