Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

As is known, algorithms and equations in latex are automatically numbered. When using \againframe though, incrementing such number is undesired. How to avoid this? (I still want numbering, but it will not change during each \againframe invocation).

A minimal working example is following. As can be seen, on second frame, the algorithm shows up with index number 2, rather than 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}
share|improve this question
2  
Can you please add to your question a minimal working example illustrating a case in which using \againframe increases the equation numbering? –  Gonzalo Medina Jul 25 '11 at 16:50

2 Answers 2

up vote 2 down vote accepted

To correct for the wrong algorithm number in your example, simply add

\addtocounter{algorithm}{-1}

just before calling \againframe<2>{myframe}. This is what the 2-slide output looks like - producing the same counter for each procedure:

Beamer presentation

However, in a more complicated setting where other algorithms are used in between the algorithm that is displayed again using \againframe, you could store the original counter. Here's a mockup of what this might involve:

\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
share|improve this answer

Here's a slight modification of Werner's solution where — except for two counter definitions in the preamble — the changes are localized in the initial frame (so if you have more than one \againframe you don't have to repeat the whole set of commands). I've added two additional frames with theorems which demonstrate the continued numbering. All the mechanics is concentrated in the first frame (the one to be shown again).

\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.