无法引用算法中的状态

无法引用算法中的状态

我有以下生成错误的代码

! LaTeX Error: Something's wrong--perhaps a missing \item.
l.193     \STATE
             \label{alg:line3}Sort NGs in CDR in ascending order based o...

\usepackage[noend]{algorithmic}
\usepackage{algorithm}
{\scriptsize
\begin{algorithm}
\caption{An Algorithm}
\label{alg1}
\begin{algorithmic}[1]
\REQUIRE a set of candidate data records $CDRs$
\ENSURE a set of re-segmented data records $DRs$
\STATE \label{alg:line1}Sort CDRs in ascending order based on their vertical positions
\FOR\label{alg:line2}{each CDR $\in$ CDRs}
    %the error happened at the following line 
    \STATE \label{alg:line3}Sort NGs in CDR in ascending order based on their vertical positions
%some other code
}

所以我想知道如何解决这个问题。

干杯

答案1

此行中的语法错误\FOR

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}

\begin{algorithm}
\caption{An Algorithm}
\label{alg1}
\begin{algorithmic}[1]
\REQUIRE a set of candidate data records $CDRs$
\ENSURE a set of re-segmented data records $DRs$
\STATE \label{alg:line1}Sort CDRs in ascending order based on their vertical positions
\FOR{each CDR $\in$ CDRs}\label{alg:line2}
    \STATE \label{alg:line3}Sort NGs in CDR in ascending order based on their vertical positions
\ENDFOR
\end{algorithmic}
\end{algorithm}

Reference to FOR: \ref{alg:line2}

Reference to first STATE: \ref{alg:line1}

Reference to second STATE: \ref{alg:line3}

\end{document}

参数\FOR必须紧跟在命令之后,应该\label在命令之后。

在此处输入图片描述

相关内容