我在以下 latex 代码中遇到错误。错误是多出或缺少 $ 或 }

我在以下 latex 代码中遇到错误。错误是多出或缺少 $ 或 }
\documentclass{article}
\usepackage{algorithm2e,algorithmicx,mathtools}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}

\begin{document}

\begin{algorithm}
\begin{algorithmic}
\State 1. Find all possible Euclidean distances between received symbols.\\
$d_{i} =\abs{r_i-r_j}  , \mkern30mu$
$\begin{cases}
s.t. \mkern15mu j = i+1  ,\\
i \in\{1,…,(M-1)\}, \text{and} \\
j \in\{2,…,M\}
\end{cases}$\\
\Statex
\State 2. Sort all $d_{i}=[d_{1},...,d_{M-1}]$ in ascending order. Assuming initialized ascending order as,\\
$[d_{M-1}$ <, ... ,< $d_{1}]$.
\Statex
\caption{: Algorithm for best probability allocation order in 4-CSK-1PD} 
 \label{alg:1}
\end{algorithmic}
\For{$i$ \leq \mkern8mu $M$} {
    \If{$r_i\in \{d_{1}\} \mkern10mu only$ } 
        { $symbol \mkern15mu r_i \gets p_1$}
    \ElseIf {$r_i\in \{d_{1},d_{2}\} \mkern10mu$ }
        {$symbol\mkern15mu r_i\gets p_2$}
    \ElseIf {$r_i\in \{d_{M-1},d_{M-2}\} \mkern10mu$ }
        { $symbol \mkern15mu r_i\gets p_3$}
    \ElseIf {$r_i\in \{d_{M-1}\} \mkern10mu$ }
        { $symbol\mkern15mu r_i\gets p_4$}
        }
    \Return{(Symbols with assigned probabilities)}
\end{algorithm}

\end{document}

答案1

在下面这一行中\end{algorithmic},有一个语法错误

\For{$i$ \leq \mkern8mu $M$} {

这些指令\leq\mkern 必须发生在数学模式中。

因此,将行更改为

\For{$i \leq \mkern8mu M$} {

或者更好的是,

\For{$i \leq M \mkern8mu$} {

另外,你还应该改变

$[d_{M-1}$ <, ... ,< $d_{1}]$

$[d_{M-1} < \dots < d_{1}]$

相关内容