未定义的控制序列 \Elsif

未定义的控制序列 \Elsif

以下代码在 latex 中的多个 else if 语句中给出了错误:“未定义控制序列.l.36 \Elsif {$ -3 <=$ polarity of $car_dictionary[fd] < -2 $}:”。请尽快帮助我。

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\title{Euclid's Algorithm: An example of how to write algorithms in \LaTeX{}}

\author{write\LaTeX{}}

\date{\today}

\begin{document}
\maketitle

\section{Example Algorithm}

Algorithms can be included using the commands as shown in algorithm \ref{alg:euclid}.

\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\For {each field fd in car\_dictionary} :
            \If{ polarity of $car\_dictionary[fd] < -3 $ }:
            \State {add "EXTREME BAD" to car\_dictionary[fd];} 
            \Elsif{$ -3  <=$ polarity of $car\_dictionary[fd] < -2 $}:
            \State add "VERY BAD" to car\_dictionary[fd];
            \ElsIf{$ -2  <=$ polarity of $car\_dictionary[fd] < -1 $}:
            \State add "BAD" to car\_dictionary[fd];
            \ElsIf{$ -1  <=$ polarity of $car\_dictionary[fd] < 0 $}:
            \State add "NOT GOOD" to car\_dictionary[fd];
            \ElsIf{polarity of $car\_dictionary[fd] == 0 $}:
            \State add "NEUTRAL" to car\_dictionary[fd];
            \ElsIf{$ 0  < $ polarity of $car\_dictionary[fd] <= 2 $}:
            \State add "GOOD" to car\_dictionary[fd];
            \ElsIf{$ 2  < $ polarity of $car\_dictionary[fd] <= 5 $}:
            \State add "SATISFACTORY" to car\_dictionary[fd];
            \ElsIf{$ 5  < $ polarity of $car\_dictionary[fd] <= 7 $}:
            \State add "VERY GOOD" to car\_dictionary[fd];
            \Else
            \State add "EXCELLENT" to car\_dictionary[fd];
            \EndIf
            \EndFor

\end{algorithmic}
\end{algorithm}

\end{document}

答案1

\ElsIf不需要\Elsif

在此处输入图片描述

\documentclass{article}

\usepackage[noend]{algpseudocode}

\newcommand{\cardict}[1]{\mbox{car\_dictionary[#1]}}

\begin{document}

\begin{algorithmic}[1]
  \For {each field fd in car\_dictionary}:
    \If{ polarity of $\cardict{fd} < -3 $ }:
      \State {add "EXTREME BAD" to \cardict{fd};} 
    \ElsIf{$ -3  <=$ polarity of $\cardict{fd} < -2 $}:
      \State add "VERY BAD" to \cardict{fd};
    \ElsIf{$ -2  <=$ polarity of $\cardict{fd} < -1 $}:
      \State add "BAD" to \cardict{fd};
    \ElsIf{$ -1  <=$ polarity of $\cardict{fd} < 0 $}:
      \State add "NOT GOOD" to \cardict{fd};
    \ElsIf{polarity of $\cardict{fd} == 0 $}:
      \State add "NEUTRAL" to \cardict{fd};
    \ElsIf{$ 0  < $ polarity of $\cardict{fd} <= 2 $}:
      \State add "GOOD" to \cardict{fd};
    \ElsIf{$ 2  < $ polarity of $\cardict{fd} <= 5 $}:
      \State add "SATISFACTORY" to \cardict{fd};
    \ElsIf{$ 5  < $ polarity of $\cardict{fd} <= 7 $}:
      \State add "VERY GOOD" to \cardict{fd};
    \Else
      \State add "EXCELLENT" to \cardict{fd};
    \EndIf
  \EndFor
\end{algorithmic}

\end{document}

相关内容