众所周知,LaTeX 中的算法和方程式会自动编号。但是使用时,不希望增加这些数字。如何避免这种情况?(我仍然想要编号,但每次调用\againframe
时它不会改变)。\againframe
下面是一个最小工作示例。可以看出,在第二帧中,算法显示索引号2
,而不是1
。
\documentclass{beamer}
\usepackage{beamerthemesplit}
\usepackage{algorithm}
\usepackage[noend]{algorithmic}
\usepackage{amsmath}
\floatname{algorithm}{Procedure}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\begin{document}
\frame<1>[label=myframe]
{
\begin{algorithm}[H]
\caption{foobar()}
\begin{algorithmic}[1]
\alt<1>
{ \STATE run() }
{ \STATE \textbf{run()} }
\end{algorithmic}
\end{algorithm}
}
\againframe<2>{myframe}
\end{document}
答案1
为了纠正示例中错误的算法编号,只需添加
\addtocounter{algorithm}{-1}
在调用之前\againframe<2>{myframe}
。这是 2 张幻灯片的输出,为每个过程生成相同的计数器:
但是,在更复杂的设置中,如果在再次使用显示的算法之间使用了其他算法\againframe
,则可以存储原始计数器。以下是这可能涉及的内容的模型:
\newcounter{storealgcounter}% Temporary storage for algorithm counter
\newcounter{algfoobar}% Holds the algorithm counter for algorithm 'foobar'
\setcounter{algfoobar}{\value{algorithm}}% Store algorithm 'foobar' counter
\frame<1>[label=myframe]{%
\begin{algorithm}[H]
\caption{foobar()}
\begin{algorithmic}[1]
\alt<1>
{ \STATE run() }
{ \STATE \textbf{run()} }
\end{algorithmic}
\end{algorithm}
}
\frame
... % some frames with algorithms
\frame
\setcounter{storealgcounter}{\value{algorithm}}% Temporarily store algorithm counter
\setcounter{algorithm}{\value{algfoobar}}% Recall algorithm 'foobar' counter
\againframe<2>{myframe}
\setcounter{algorithm}{\value{storealgcounter}}% Restore algorithm counter
答案2
这是对 Werner 解决方案的轻微修改,除了序言中的两个计数器定义外,更改都集中在初始帧中(因此,如果您有多个,则\againframe
不必重复整个命令集)。我添加了两个附加帧,其中的定理演示了连续编号。所有机制都集中在第一帧中(将再次显示)。
\documentclass{beamer}
\usepackage{beamerthemesplit}
\usepackage{algorithm}
\usepackage[noend]{algorithmic}
\usepackage{amsmath}
\floatname{algorithm}{Procedure}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\newcounter{inialg}
\newcounter{savealg}
\begin{document}
\frame<1>[label=myframe]
{
\alt<1>{\setcounter{inialg}{\value{algorithm}}}{%
\setcounter{savealg}{\value{algorithm}}%
\setcounter{algorithm}{\value{inialg}}%
}
\begin{algorithm}[H]
\caption{foobar()}
\begin{algorithmic}[1]
\alt<1>
{ \STATE run() }
{ \STATE \textbf{run()} }
\end{algorithmic}
\end{algorithm}
\only<2->{\setcounter{algorithm}{\value{savealg}}}
}
\frame
{
Another algorithm in between.
\begin{algorithm}[H]
\caption{havefun()}
\begin{algorithmic}[1]
\STATE enjoy()
\end{algorithmic}
\end{algorithm}
}
\againframe<2>{myframe}
\frame
{
Yet another algorithm.
\begin{algorithm}[H]
\caption{relax()}
\begin{algorithmic}[1]
\STATE sleep()
\end{algorithmic}
\end{algorithm}
}
\end{document}
答案3
您可以使用\resetcounteronoverlays{algorithm}
将计数器添加到每个覆盖重置的计数器列表中:
\documentclass{beamer}
\usepackage{beamerthemesplit}
\usepackage{algorithm}
\usepackage[noend]{algorithmic}
\floatname{algorithm}{Procedure}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\resetcounteronoverlays{algorithm}
\begin{document}
\begin{frame}<1>[label=myframe]
\begin{algorithm}[H]
\caption{foobar()}
\begin{algorithmic}[1]
\alt<1>
{ \STATE run() }
{ \STATE \textbf{run()} }
\end{algorithmic}
\end{algorithm}
\end{frame}
\againframe<2>{myframe}
\end{document}