[Tex/LaTex] Problem with counter and \pause

beamercountersoverlays

I am trying to create a manual continuation annotation for my presentations (because I need more flexibility than provided by the automatic splitting feature of beamer).

What I thought of is, for each sequence of slides create a counter and then increment and print its value on each successive slide of the sequence.

If I do that "manually", everything works fine.

Then I tried to automate things a little bit more, as shown below. The countslides command takes an argument, constructs a counter using the argument for its name (if it does not exist) and then increments and prints its value.

Unfortunately, the code produces an error "Missing number, treated as zero" on the first slide (even though the result is correct). The error goes away if I remove the \pause command on this slide (or if I remove the \resetcounteronoverlays from my command definition, but I cannot afford this).

Maybe my code is wrong, but I cannot see how (and it has worked fine in all other cases).

\documentclass{beamer}
\usepackage{ifthen}
\begin{document}

\makeatletter
\newcommand{\countslides}[1]{%
  \ifthenelse{\expandafter\isundefined\csname c@cnt#1\endcsname}%
    {\newcounter{cnt#1}%
     \resetcounteronoverlays{cnt#1}%
    }%
    {}%
  \stepcounter{cnt#1}%
  {~\footnotesize (\arabic{cnt#1})}%
}
\makeatother

\begin{frame}
\frametitle{Hello \countslides{abc}}
A
\pause
B
\end{frame}

\begin{frame}
\frametitle{Bye \countslides{abc}}
C
\pause
D
\end{frame}

\end{document}

Best Answer

Solution 0: if you can live with splitting off the initial definition:

\documentclass{beamer}

\makeatletter
\newcommand\countable[1]{%
  \newcounter{cnt#1}%
  \resetcounteronoverlays{cnt#1}%
}
\newcommand{\countslides}[1]{%
  \stepcounter{cnt#1}%
  {~\footnotesize (\arabic{cnt#1})}%
}
\makeatother
\begin{document}


\countable{abc}
\begin{frame}
\frametitle{Hello \countslides{abc}}
A
\pause
B
\end{frame}

\begin{frame}
\frametitle{Bye \countslides{abc}}
C
\pause
D
\end{frame}

\end{document}

A solution that strikes me as somewhat cleaner would be this: you give the frames in question the contgroup=... key, and they should all behave as if they were broken automatically, i.e. the normal continuation title templates are applied.

\documentclass{beamer}

\makeatletter

\newcommand\handlecontgroup[1]{%
  \only<1>{%
    \ifcsname ums@cntgroup@#1\endcsname
      \relax
    \else
      \expandafter\gdef\csname ums@cntgroup@#1\endcsname{0}%
    \fi
    \beamer@autobreakcount=\csname ums@cntgroup@#1\endcsname\relax
    \advance\beamer@autobreakcount by 1\relax
    \expandafter\xdef\csname ums@cntgroup@#1\endcsname{%
      \the\beamer@autobreakcount}%
  }%
}
\define@key{beamerframe}{contgroup}{\handlecontgroup{#1}}%
\makeatother
\begin{document}


\begin{frame}[contgroup=abc]
\frametitle{Hello}
A
\pause
B
\end{frame}

\begin{frame}[contgroup=abc]
\frametitle{Bye}
C
\pause
D
\end{frame}

\begin{frame}
\frametitle{Bye}
C
\pause
D
\end{frame}

\begin{frame}[contgroup=abc]
\frametitle{Bye}
C
\pause
D
\end{frame}

\end{document}