[Tex/LaTex] Making algorithm2e environments “overlay aware” in beamer

algorithm2ebeamernumbering

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.

Best Answer

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