6

The following latex source generates two pdf pages. Page 1 has title "Exercise 1 I" and page 2 "Exercise 2 II".

\documentclass[t]{beamer}
\let\Tiny=\tiny
\newcounter{mycounter}
\setcounter{mycounter}{0}
\def\exercise{\refstepcounter{mycounter}Exercise \arabic{mycounter}}
\begin{document}

\begin{frame}[allowframebreaks]
  \frametitle{\exercise}  % this seems expanded in each frame

  ABC

  \framebreak

  DEF
\end{frame}

\end{document}

The resulting page 1 is

Exercise 1 I
  ABC

and page 2

Exercise 2 II  % I want Exercise 1 II
  DEF

I want "Exercise 1 II" on page 2 instead. One way is to delete \refstepcounter{mycounter} from the \exercise macro and to manually do \refstepcounter outside. This is nuisance, and I would make mistakes. Would there be a better way?

2
  • 2
    My guess is that you cannot prevent it, since on the one hand this is how beamer works and one the other hand this is what one usually wants, e.g. when creating overlays. I suggest to separate incrementing the counter and typesetting the frametitle: Move the \refstepcounter command before \begin{frame}, then it is executed only once.
    – gernot
    Oct 15 '16 at 14:55
  • Disappointed, but good to know. Thanks.
    – chan1142
    Oct 15 '16 at 15:12
6

You can use the counter \beamer@autobreakcount and do the stepping only if this counter has a less than 2 (it will be normally zero, but it becomes 1 when allowframebreaks is issued).

\documentclass[t]{beamer}
\let\Tiny=\tiny

\newcounter{mycounter}

\makeatletter
\newcommand\exercise{%
  \ifnum\beamer@autobreakcount<2
    \refstepcounter{mycounter}%
  \fi
  Exercise \themycounter
}
\makeatother

\begin{document}

\begin{frame}[allowframebreaks]
\frametitle{\exercise}  % this seems expanded in each frame

  ABC

  \framebreak

  DEF
\end{frame}

\begin{frame}
\frametitle{\exercise}

GHI

\end{frame}

\end{document}

enter image description here

5

@gernot: Never say never with LaTeX :)

The basic idea is to test if the current page is the first page of a frame and only then to increase the counter.

\documentclass[t]{beamer}
\let\Tiny=\tiny
\newcounter{mycounter}
\setcounter{mycounter}{0}

\makeatletter
\def\exercise{%
    \ifnum\insertframestartpage=\thepage
        \refstepcounter{mycounter}
    \fi
    Exercise \arabic{mycounter}%
}
\makeatother

\begin{document}

\begin{frame}[allowframebreaks]
  \frametitle{\exercise}  % this seems expanded in each frame

  ABC

  \framebreak

  DEF
\end{frame}

\end{document}

enter image description here

1
  • Thanks! The \ifnum\insertframestartpage=\thepage trick is excellent. I wish I could accept multiple answers.
    – chan1142
    Oct 16 '16 at 1:32

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.