我有一个要使用的模板,并想在其中添加一个算法。模板和算法的简短版本将是
\RequirePackage{fix-cm,cmap}
\documentclass[11pt, a4paper,abstracton,numbers=noenddot,listof=totoc,bibliography=totoc,twoside,openright,cleardoublepage=plain]{scrreprt}
% Algorithms
\usepackage[ruled, vlined, linesnumbered,algosection,algo2e]{algorithm2e}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{algorithm}[theorem]{Algorithm}
\newenvironment{example}{\begin{quote}{\bf Example:}}{\end{quote}}
%% here your document starts
\begin{document}
\begin{algorithm}
\caption{Euclid's algorithm}\label{euclid}
\Procedure{Euclid}{$a,b$}\Comment{The gcd of $a$ and $b$}
\State $r \gets a \bmod b$
\While{$r \neq 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
\end{algorithm}
\listofalgorithmes
\end{document}
如果我像这样尝试,我会收到大量错误消息:
\caption outside of float
Undefined control sequence.\Procedure
....
我究竟做错了什么?
答案1
你混淆了算法包语法。你使用的算法示例代码源自algorithmicx
包裹当你正在加载时algorithm2e
。以下是来自algorithm2e
文档:
\documentclass{scrreprt}
\usepackage[ruled,vlined,linesnumbered,algosection,algo2e]{algorithm2e}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{algorithm}[theorem]{Algorithm}
\begin{document}
\begin{algorithm2e}
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm2e}
\listofalgorithmes
\end{document}
algorithm
由于没有提供有关模板的上下文,因此不完全清楚为什么需要类似定理的环境。