algorithm/algpseudocode 中的 For(或 ForAll)循环存在问题:缺少插入的 $

algorithm/algpseudocode 中的 For(或 ForAll)循环存在问题:缺少插入的 $

我目前有一个无法解决的问题。这是我的代码:

\begin{algorithm}
\caption{Feedback system}
\begin{algorithmic}[1]
\State $expert$: object describing the expert (name, laterality and path to data)
\State $student$: object describing the student (name, laterality and path to data)
\Procedure{Feedback}{$expert$, $student$}
    \State $D_{exp} \gets$ all data from $expert$
    \State $D_{std} \gets$ all data from $student$
    \State $P \gets$ all the data to process for each problem

    \If{$normalisation$ == true}
        \State $P \gets$ normalised($P$)
    \EndIf
    \State $X_{d_{exp}} \gets$ expert's data repartition

    \ForAll{$p_i$ in $P$}
        \State $d_{exp} \gets$ get_data($p_i$, $X_{d_{exp}}$)
    \EndFor 

    \State clustering_result $\gets$ run_clustering
    \State student_centroid $\gets$ get_centroid($D_{std}$)

 \EndProcedure
 \end{algorithmic}
 \label{algorithm_Feedback}
\end{algorithm}

我的进口是

\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing, dottedtoc]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{url}
\usepackage[style=alphabetic]{biblatex}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{pdflscape}
\usepackage{makecell}
\usepackage{algorithm, algpseudocode}
\usepackage{enumitem}
\usepackage{colortbl}

我的问题出在 ForAll 循环中。基本上,它告诉我下一行缺少一个 $。如果我删除 之后的所有代码\ForAll,甚至删除循环\State中的ForAll(显然保留\EndFor),它编译时不会出现任何问题。我尝试仅删除循环\State内部的ForAll\State之后的\EndFor,但没有成功。

长话短说,这是有效的

\ForAll{$p_i$ in $P$}
\EndFor 

这并没有

\ForAll{$p_i$ in $P$}
    \State $d_{exp} \gets$ get_data($p_i$, $X_{d_{exp}}$)
\EndFor 
\ForAll{$p_i$ in $P$}
    \State $d_{exp} \gets$ get_data($p_i$, $X_{d_{exp}}$)
\EndFor 

\State clustering_result $\gets$ run_clustering
\State student_centroid $\gets$ get_centroid($D_{std}$)

有人能指出我哪里出了问题吗?我真的不明白,而且我认为我的语法没问题?我还没找到和我遇到同样问题的人。如果这个问题已经有人回答了或者这个错误真的很明显而且很愚蠢,我很抱歉。

干杯

编辑:这是一个可编译的示例:

\documentclass[
  % Replace twoside with oneside if you are printing your thesis on a single side
  % of the paper, or for viewing on screen.
  %oneside,
  oneside,
  11pt, a4paper,
  footinclude=true,
  headinclude=true,
  cleardoublepage=empty
]{scrbook}

\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing, dottedtoc]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{url}
\usepackage[style=alphabetic]{biblatex}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{pdflscape}
\usepackage{makecell}
\usepackage{algorithm, algpseudocode}
\usepackage{enumitem}
\usepackage{colortbl}

\title{Giga titre qui tue et qui donne envie de lire alors qu'en fait c'est chiant}
\author{Quentin Couland\\ Directeur de thèse : \textbf{Sébastien George}\\Co-encadrant : \textbf{Ludovic Hamon}}

\begin{document}

\begin{algorithm}
\caption{Feedback system}
\begin{algorithmic}[1]
\State $expert$: object describing the expert (name, laterality and path to data)
\State $student$: object describing the student (name, laterality and path to data)
\Procedure{Feedback}{$expert$, $student$}
    \State $D_{exp} \gets$ all data from $expert$
    \State $D_{std} \gets$ all data from $student$
    \State $P \gets$ all the data to process for each problem

    \If{$normalisation$ == true}
        \State $P \gets$ normalised($P$)
    \EndIf
    \State $X_{d_{exp}} \gets$ expert's data repartition

    \ForAll{$p_i$ in $P$}
        \State $d_{exp} \gets$ get_data($p_i$, $X_{d_{exp}}$)
    \EndFor 

    \State clustering_result $\gets$ run_clustering
    \State student_centroid $\gets$ get_centroid($D_{std}$)

 \EndProcedure
 \end{algorithmic}
 \label{algorithm_Feedback}
\end{algorithm}

\end{document}

答案1

您需要\_在文本模式下执行。

它必须是:

\State $d_{exp} \gets$ get\_data($p_i$, $X_{d_{exp}}$)

代替

\State $d_{exp} \gets$ get_data($p_i$, $X_{d_{exp}}$)
\documentclass[
  % Replace twoside with oneside if you are printing your thesis on a single side
  % of the paper, or for viewing on screen.
  %oneside,
  oneside,
  11pt, a4paper,
  footinclude=true,
  headinclude=true,
  cleardoublepage=empty
]{scrbook}

\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing, dottedtoc]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{url}
\usepackage[style=alphabetic]{biblatex}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{pdflscape}
\usepackage{makecell}
\usepackage{algorithm, algpseudocode}
\usepackage{enumitem}
\usepackage{colortbl}

\title{Giga titre qui tue et qui donne envie de lire alors qu'en fait c'est chiant}
\author{Quentin Couland\\ Directeur de thèse : \textbf{Sébastien George}\\Co-encadrant : \textbf{Ludovic Hamon}}

\begin{document}

\begin{algorithm}
\caption{Feedback system}
\begin{algorithmic}[1]
\State $expert$: object describing the expert (name, laterality and path to data)
\State $student$: object describing the student (name, laterality and path to data)
\Procedure{Feedback}{$expert$, $student$}
    \State $D_{exp} \gets$ all data from $expert$
    \State $D_{std} \gets$ all data from $student$
    \State $P \gets$ all the data to process for each problem

    \If{$normalisation$ == true}
        \State $P \gets$ normalised($P$)
    \EndIf
    \State $X_{d_{exp}} \gets$ expert's data repartition

    \ForAll{$p_i$ in $P$}
        \State $d_{exp} \gets$ get\_data($p_i$, $X_{d_{exp}}$)
    \EndFor 

    \State clustering\_result $\gets$ run\_clustering
    \State student\_centroid $\gets$ get\_centroid($D_{std}$)

 \EndProcedure
 \end{algorithmic}
 \label{algorithm_Feedback}
\end{algorithm}

\end{document}

结果

相关内容