我为文档中的每个部分使用单独的 .tex 文件。其中一个文档需要编写算法。我使用algorithm2e
包来执行此操作。
这是我的 MWE(算法取自本网站):
%!TeX root = Section2
%Section 2
\documentclass[crop=false]{standalone}
\usepackage{enumerate,tikz}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}
\begin{document}
\subsection{Simple algorithm}
\begin{algorithm}
\KwIn{A sequence of integers $\langle a_1, a_2, \ldots, a_n \rangle$}
\KwOut{The index of first location witht he same value as in a previous location in the sequence}
$location \gets 0$\;
$i \gets 2$\;
\While{$i \leq n$ \textbf{and} $location = 0$}{
$j \gets 1$\;
\While{$j < i$ \textbf{and} $location = 0$}{
% The "u" before the "If" makes it so there is no "end" after the statement, so the else will then follow
\uIf{$a_i = a_j$}{
$location \gets i$\;
}
\Else{
$j \gets j + 1$\;
}
}
$i \gets i + 1$\;
}
\Return{location}\;
\caption{{\sc FindDuplicate}}
\label{algo:duplicate}
\end{algorithm}
\subsection{Advanced algorithm}
\end{document}
缩进和行号都乱了。下面是我收到的错误消息,我猜主要问题是所谓的\prevdepth
命令,它在某个地方自动使用并导致与其他环境不兼容。
! You can't use `\prevdepth' in horizontal mode.
\nointerlineskip ->\prevdepth
-\@m \p@
l.70 \Return
{$max$}\;
! Missing $ inserted.
<inserted text>
$
l.70 \Return
{$max$}\;
! Missing number, treated as zero.
<to be read again>
\advance
l.70 \Return
{$max$}\;
! Illegal unit of measure (pt inserted).
<to be read again>
\advance
l.70 \Return
{$max$}\;
! You can't use `\moveright' in math mode.
\algocf@push ...dvance \skiptotal by #1\moveright
#1
l.70 \Return
{$max$}\;
! Missing number, treated as zero.
<to be read again>
\hbox
l.70 \Return
{$max$}\;
! Illegal unit of measure (pt inserted).
<to be read again>
\hbox
l.70 \Return
{$max$}\;
! Arithmetic overflow.
<recently read> \p@
l.70 \Return
{$max$}\;
Package relsize Warning: Size requested is too small.
(relsize) Using 5.0pt instead on input line 70.
! Improper `at' size (0.0pt), replaced by 10pt.
<to be read again>
relax
l.70 \Return
{$max$}\;
! You can't use `\prevdepth' in horizontal mode.
\nointerlineskip ->\prevdepth
-\@m \p@
l.70 \Return
{$max$}\;
! Missing $ inserted.
<inserted text>
$
l.70 \Return
{$max$}\;
! Missing number, treated as zero.
<to be read again>
\advance
l.70 \Return
{$max$}\;
! Illegal unit of measure (pt inserted).
<to be read again>
\advance
l.70 \Return
{$max$}\;
! You can't use `\moveright' in math mode.
\algocf@push ...dvance \skiptotal by #1\moveright
#1
l.70 \Return
{$max$}\;
! Missing number, treated as zero.
<to be read again>
\hbox
l.70 \Return
{$max$}\;
! Illegal unit of measure (pt inserted).
<to be read again>
\hbox
l.70 \Return
{$max$}\;
! Arithmetic overflow.
<recently read> \p@
l.70 \Return
{$max$}\;
Package relsize Warning: Size requested is too small.
(relsize) Using 5.0pt instead on input line 70.
! Missing $ inserted.
<inserted text>
$
l.70 \Return
{$max$}\;
! 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.70 \Return
{$max$}\;
! Missing $ inserted.
<inserted text>
$
l.70 \Return
{$max$}\;
! 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.70 \Return
{$max$}\;
! 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.70 \Return{$max$}\;
! 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.71 \caption{{\sc Max} finds the maximum number}
! 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.73 \end{algorithm}
Overfull \hbox (15.0pt too wide) in paragraph at lines 73--73