使用算法时出现 Latex 错误

使用算法时出现 Latex 错误

使用 winedt 和 miktex 编译算法时出现以下错误:

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.42 \end{algorithmic}

这是我的源代码:

\documentclass{article}
\textheight=9.75in
\textwidth 6.55in
\hoffset=-.75in
\voffset=-.5in
\headsep=-.25in

\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage{algc}
\usepackage{algcompatible}
\usepackage{algpseudocode}

\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\algnewcommand{\LINECOMMENT}[1]{\STATE\(\triangleright\) #1}


\begin{document}

\floatname{algorithm}{Algorithm}
\begin{algorithm}
\caption{Cuckoo Search Algorithm}
\begin{algorithmic}[1]
\REQUIRE{A set $N = \{1,2,\ldots, n\}$ of $n$ jobs, a set $M = \{1,2,\ldots ,m\}$ of $m$ identical parallel machines, processing time $p_i$ for each job $i \in N$, MaxGeneration, and stopping criterion.}
\STATE Generate an initial population of $R$ host nests $x_i$, $(i=1,\ldots ,R)$.
\WHILE{$t < $ MaxGeneration or stopping criterion}
  \STATE Obtain a cuckoo randomly by Levy flights.
  \STATE Evaluate its quality/fitness $F_{i}$.
  \STATE Choose a nest (say, $j$) randomly from the population of $R$ and compute its quality/fitness as $F_{j}$.
   \IF {$F_{i}> F_{j}$,}
    \STATE Replace $j$ with the new solution.
    % \ELSE
     \ENDIF
    \STATE A fraction $(P_a)$ of worst nests from the population are abandoned and new ones are constructed.
    \STATE Retain the best quality solutions (or nests with quality solutions).
    \STATE Sort the solutions and find the current best.
\ENDWHILE
\ENSURE{The current best schedule of $n$ jobs and its corresponding MS.}
\end{algorithmic}
\label{CSA}
\end{algorithm}
\end{document}

答案1

该错误是由于您使用了“兼容”模式algorithmicx但最后加载了包algpseudocode

你必须加载仅有的algcompatible,即使用

\usepackage{algorithm}
\usepackage{algcompatible}

代替

\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage{algc}
\usepackage{algcompatible}
\usepackage{algpseudocode}

相关内容