显示算法时出现错误消息

显示算法时出现错误消息

下面是 MWE。我正在写我的学士论文,这些错误困扰了我好长一段时间。它们几乎产生了正确的输出,但行数始终为零,我得到了这些错误。错误、我的包和我的代码如下:

! 缺少插入的 \endcsname。\ALG@currentblock@0 l.35 \If {$Med(q_{-i}) > p_i$} 标记的控制序列不应出现在 \csname 和 \endcsname 之间。

! 额外的 \endcsname。 \ALG@makebeginrepeat ...\ALG@thisblock \endcsname \relax \def \ALG@thisblock... l.35 \If {$Med(q_{-i}) > p_i$} 我忽略了这一点,因为我没有执行 \csname。

!包 algorithmicx 错误:有些块未关闭!!!。

请参阅 algorithmicx 包文档以获取解释。输入 H 可立即获得帮助。...

l.57 \end{algorithmic} 此错误消息由 \errmessage 命令生成,因此我无法提供任何明确帮助。假设您是赫尔克里·波洛:检查所有线索,并按顺序和方法推断真相。

软件包:

\usepackage[latin1]{inputenc}  \usepackage[T1]{fontenc} 
\usepackage{lmodern} 
\usepackage[english]{babel} 
\usepackage{graphicx} 
\usepackage{amsmath} 
\usepackage{amssymb} 
\usepackage{amsthm}
\usepackage{multirow} 
\usepackage{algorithm}
\usepackage{algpseudocode} 
\usepackage{tikz} 
\usepackage{array} 
\usepackage{scrhack} 
\usepackage{listings} 
\usepackage{pdfpages} 
\usepackage{placeins} 
\usepackage{textcomp} 
\usepackage{comment} 
\usepackage[raiselinks=true, bookmarks=true, bookmarksopenlevel=1, bookmarksopen=true, bookmarksnumbered=true, hyperindex=true, plainpages=false,
                                                pdfpagelabels=true, pdfborder={0 0 0.5}, colorlinks=false, linkbordercolor={0 0.61 0.50}, citebordercolor={0 0.61 0.50}, breaklinks=true]{hyperref}

代码:

\begin{algorithm} \caption{Best Response under median-rule} \begin{algorithmic}   \If {$Med(q_{-i}) > p_i$}   \If {$n_q$ is even}  \If {$q_{Med-} > p_i$}
    \State ideal vote $\leq q_{Med-}$    \ElsIf{$q_{Med-} \leq p_i$}        
    \State ideal vote $= p_i$   \ElsIf{$n_q$ is odd}
    \If {$\frac{q_{Med-} + Med(q_{-i})}{2} > p_i$}
     \State ideal vote $\leq q_Med-$
    \ElsIf{$\frac{q_{Med-} + Med(q_{-i})}{2} \leq p_i$} 
     \State ideal vote $= Med(q_{-i}) - 2*(Med(q_{-i}) - p_i)$            \ElsIf{$Med(q_{-i}) \leq p_i$}       \If {$n_q$ is even}    \If {$q_{Med+} < p_i$}
     \State ideal vote $\geq q_{Med+}$    \ElsIf{$q_{Med+} \geq p_i$}   
    \State ideal vote $= p_i$               \ElsIf{$n_q$ is odd}
    \If {$\frac{q_{Med+} + Med(q_{-i})}{2} < p_i$}
     \State ideal vote $\geq q_{Med+}$
    \ElsIf{$\frac{q_{Med+} + Med(q_{-i})}{2} \geq p_i$}
     \State ideal vote $= Med(q_{-i}) + 2*(p_i - Med(q_{-i}))$      
 \end{algorithmic} \end{algorithm}

最小工作示例:

\documentclass{thesis}
\usepackage{algorithm}
%\usepackage{algorithmic}
%\usepackage{algorithmicx}
\usepackage{algpseudocode}
\begin{document}
\pagenumbering{roman}

\begin{algorithm}
\caption{Best Response under median-rule}
\begin{algorithmic} 
        \If {$Med(q_{-i}) > p_i$}
            \If {$n_q$ is even}
                        \If {$q_{Med-} > p_i$}
                            \State ideal vote $\leq q_{Med-}$
                        \ElsIf{$q_{Med-} \leq p_i$}     
                            \State ideal vote $= p_i$
                        \EndIf
            \ElsIf{$n_q$ is odd}
                    \If {$\frac{q_{Med-} + Med(q_{-i})}{2} > p_i$}
                            \State ideal vote $\leq q_Med-$
                    \ElsIf{$\frac{q_{Med-} + Med(q_{-i})}{2} \leq p_i$} 
                            \State ideal vote $= Med(q_{-i}) - 2*(Med(q_{-i}) - p_i)$       
                    \EndIf
            \EndIf                  
        \ElsIf{$Med(q_{-i}) \leq p_i$}  
            \If {$n_q$ is even}
                \If {$q_{Med+} < p_i$}
                    \State ideal vote $\geq q_{Med+}$
                \ElsIf{$q_{Med+} \geq p_i$} 
                        \State ideal vote $= p_i$   
                \EndIf                  
            \ElsIf{$n_q$ is odd}
                    \If {$\frac{q_{Med+} + Med(q_{-i})}{2} < p_i$}
                        \State ideal vote $\geq q_{Med+}$
                    \ElsIf{$\frac{q_{Med+} + Med(q_{-i})}{2} \geq p_i$}
                        \State ideal vote $= Med(q_{-i}) + 2*(p_i - Med(q_{-i}))$       
                    \EndIf
            \EndIf
        \EndIf              
\end{algorithmic}
\end{algorithm}

\addcontentsline{toc}{chapter}{\bibname}

\bibliographystyle{apalike} % german style

% Use IEEEtran for numeric references
%\bibliographystyle{IEEEtranSA})

\bibliography{thesis}

\end{document}

答案1

我找到了问题所在,thesis.cls 包含的包algorithmicalgpseudocode同时产生了错误。我将其取出algorithmic,现在一切正常。

感谢您的评论帮助我找到这一点。

相关内容