# Problem with counter and \pause

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}

-
To a zeroth approximation, I'd guess: \newcounter is global. \pause will cause the frame contents to be typeset more than once. That sounds like something that could conflict. – Ulrich Schwarz Jul 19 '13 at 16:58
Do you need fancy nesting of the continuation frames that would require you to have several counters for this? I'm seeing if we can just expose beamer's "continuation" mechanic to handle manual breaks. – Ulrich Schwarz Jul 19 '13 at 17:10
@UlrichSchwarz I do have one situation where the sequence of slides is interrupted, so the simple splitting of beamer is not enough. Also I would like to implement a notation "(1/3)" on the title of the slides; I have done so using the totcount package with the above code, but I will see how this can be implemented using your (second) solution. – nplatis Jul 21 '13 at 6:56

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
\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}

-
The first solution is simple and nice, but as a programmer I would always prefer to automate things as much as possible :-) so the second solution is indeed cleaner. – nplatis Jul 21 '13 at 6:59