6

What is the right away to make algorithms overlay aware in beamer?

When using algorithm2e in a beamer presentation, an algorithm on a frame with overlays can get more than one number.

For example, this code produces two slides, on which the same algorithm gets two different numbers.

\documentclass{beamer}
\usepackage[boxruled,vlined,linesnumbered]{algorithm2e}
\usepackage{hyperref}  

\begin{document}
\begin{frame}\frametitle{my algorithm}
  \begin{algorithm}[H] \caption{my algorithm}
    do this\;
    then do that\;
  \end{algorithm}

  \pause 

  Some explanations.
\end{frame}
\end{document}

The issue appears to be that the algorithm environment is not "overlay aware". I tried using

\resetcountonoverlays{algorithm}

but that just stopped the file from compiling entirely.

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Feb 6 '15 at 20:01
6

The right counter is algocf and not algorithm. Also you have to use \resetcounteronoverlays instead of \resetcountonoverlays since algocf is defined as

\newcounter{algocf}

In other words, adding the following line in your preamble

\resetcounteronoverlays{algocf}

solves the issue.

MWE:

\documentclass{beamer}
\usepackage[boxruled,vlined,linesnumbered]{algorithm2e}
\usepackage{hyperref}
\resetcounteronoverlays{algocf}

\begin{document}
\begin{frame}\frametitle{my algorithm}
  \begin{algorithm}[H] \caption{my algorithm}
    do this\;
    then do that\;
  \end{algorithm}

  \pause

  Some explanations.
\end{frame}
\end{document} 

Output

enter image description here enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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