算法块中的 \caption 命令出现此错误:缺少数字,视为零

算法块中的 \caption 命令出现此错误:缺少数字,视为零

我试图用 LaTeX 为我的算法写一个标题,但似乎出了点问题。当我编译代码时,标题中出现了一些奇怪的符号,并且缺失数字,视为零错误。(见最后的图片)然而,当我删除标题命令,代码编译得很好!如果你能帮忙,我将不胜感激。

\begin{algorithm}[H]

\SetKwInput{Var}{Variables}
\SetKwInput{Alg}{Algorithm}
\SetKwData{D}{d}\SetKwData{Eps}{eps}\SetKwData{Min}{minPts}
\SetKwData{M}{m}\SetKwData{N}{n}\SetKwData{Dist}{dist}
\SetKwData{Indices}{indices}\SetKwData{Classn}{class\_no}
\SetKwData{Nei}{neighbors}\SetKwData{Neic}{neighbor\_count}
\SetKwData{Core}{core\_neig}\SetKwData{Class}{class}
\SetKwData{Point}{point}

\SetKwFunction{Distance}{distance}
\SetKwFunction{Find}{find}
\SetKwFunction{Count}{count}
\SetKwFunction{Check}{check\_core\_neighbour}

 \KwIn{\\
 \D: the dataset\\
 \Eps: the neighbourhood distance \\
 \Min: the minimum number of points
 }
 \KwOut{\\Discovered outliers and clusters }
 \Var{\\
 \M, \N: row and column values of \D matrix, respectively\\
 \Dist: distance vector\\
 \Indices: indices that distance of points is lower than \Eps\\
 \Classn: indicates the clusters - default 1}
\BlankLine

\Alg{\\
    import the dataset into \D\\
    \For{$i \leftarrow 1$ \KwTo \M}{
        \Dist $\leftarrow \Distance(i, \D)$\\
        \Nei$\leftarrow$\Find(\Dist$\leq$\Eps)\\
        \Neic$\leftarrow$\Count(\Nei)\\
        \Core$\leftarrow$\Check(\Nei)\\
    
        \uIf{(\Neic$\geq$\Min)}{
            \Class($i$)$\leftarrow$\Classn\\
            \While{more points near $i$}{
                \Class(\Point)=\Classn
            }
            \Classn$\leftarrow$\Classn $+1$
        }
        \uElseIf{(\Neic$<$\Min$\&\&$\Core$==$true)}{
            \Class($i$)$\leftarrow 0$\tcc*[r]{border point}
        }
        \uElseIf{(\Neic$<$\Min)}{
            \Class($i$)$\leftarrow -1$\tcc*[r]{outlier point}
        }
    }
    \KwRet{\Class}
}
 \BlankLine
 \caption{DBSCAN \cite{ccelik2011anomaly}}
 \label{alg:db}
\end{algorithm} 

输出

错误

答案1

相当刻意的错误。

\SetKwData{M}{m}

不仅定义了\M,还定义了一个内部宏,\@M该宏已被 LaTeX 内核抢占。此重新定义不会检查是否\@M已定义。

在 LaTeX 内核中,\@M代表 10000,并在多个地方用于表示禁止换行或分页的惩罚值。

使用

\SetKwData{MM}{m}

\MM在算法主体中;或者选择一个完全不同的名称。

如果你觉得大胆点,可以修正一下错误的策略algorithm2e;也\SetKw存在同样的问题。

\documentclass{article}
\usepackage{algorithm2e}

\makeatletter
%%% fix the wrong code in algorithm2e
\renewcommand{\SetKwData}[2]{%
  \algocf@newcommand{algocf@kwdata@#1}[1]{\DataSty{#2(}\ArgSty{##1}\DataSty{)}}%
  \algocf@newcommand{#1}{%
    \@ifnextchar\bgroup{\csname algocf@kwdata@#1\endcsname}{\DataSty{#2}\xspace}}%
  }%
\renewcommand{\SetKw}[2]{%
        \algocf@newcommand{algocf@kw@#1}[1]{\KwSty{#2} \ArgSty{##1}}
        \algocf@newcommand{#1}{\@ifnextchar\bgroup{\csname algocf@kw@#1\endcsname}{\KwSty{#2}\xspace}}%
}%
\makeatother

\begin{document}

\begin{algorithm}[H]
\SetKwInput{Var}{Variables}
\SetKwInput{Alg}{Algorithm}
\SetKwData{D}{d}\SetKwData{Eps}{eps}\SetKwData{Min}{minPts}
\SetKwData{M}{m}\SetKwData{N}{n}\SetKwData{Dist}{dist}
\SetKwData{Indices}{indices}\SetKwData{Classn}{class\_no}
\SetKwData{Nei}{neighbors}\SetKwData{Neic}{neighbor\_count}
\SetKwData{Core}{core\_neig}\SetKwData{Class}{class}
\SetKwData{Point}{point}

\SetKwFunction{Distance}{distance}
\SetKwFunction{Find}{find}
\SetKwFunction{Count}{count}
\SetKwFunction{Check}{check\_core\_neighbour}

 \KwIn{\\
 \D: the dataset\\
 \Eps: the neighbourhood distance \\
 \Min: the minimum number of points
 }
 \KwOut{\\Discovered outliers and clusters }
 \Var{\\
 \M, \N: row and column values of \D matrix, respectively\\
 \Dist: distance vector\\
 \Indices: indices that distance of points is lower than \Eps\\
 \Classn: indicates the clusters - default 1}
\BlankLine

\Alg{\\
    import the dataset into \D\\
    \For{$i \leftarrow 1$ \KwTo \M}{
        \Dist $\leftarrow \Distance(i, \D)$\\
        \Nei$\leftarrow$\Find(\Dist$\leq$\Eps)\\
        \Neic$\leftarrow$\Count(\Nei)\\
        \Core$\leftarrow$\Check(\Nei)\\
    
        \uIf{(\Neic$\geq$\Min)}{
            \Class($i$)$\leftarrow$\Classn\\
            \While{more points near $i$}{
                \Class(\Point)=\Classn
            }
            \Classn$\leftarrow$\Classn $+1$
        }
        \uElseIf{(\Neic$<$\Min$\&\&$\Core$==$true)}{
            \Class($i$)$\leftarrow 0$\tcc*[r]{border point}
        }
        \uElseIf{(\Neic$<$\Min)}{
            \Class($i$)$\leftarrow -1$\tcc*[r]{outlier point}
        }
    }
    \KwRet{\Class}
}
\BlankLine
\caption{DBSCAN \protect\cite{ccelik2011anomaly}}
\label{alg:db}
\end{algorithm} 

\end{document}

使用更复杂的前缀而不是简单的前缀@将避免此类问题。

相关内容