6

I define a counter in beamer. But when the pause command occurs, the counter will be added automatically. How can I do to make the counter not sensitive to the pause command? The following is the tex codes for test.

\documentclass[serif,envcountsect]{beamer}

\newcounter{DefNum}
\usecounter{DefNum}
\newcommand{\defi}{\noindent\text{Definition \arabic{DefNum}}\refstepcounter{DefNum}}
\setcounter{DefNum}{1}



\begin{document}
\begin{frame}{test}
\defi

\defi


test hello ~\\ \pause
hello test \pause

\begin{itemize}
  \item aa \pause
  \item bb \pause
  \item cc
  \item \defi
\end{itemize}

\defi
\end{frame}
\end{document} 
  • Thank you for posting a working example! At the same time, much of your code does not seem to be relevant to the question you're asking here. Please limit the example to only the code required for your issue to appear. You can have a look at this guide for how to prune your code for this purpose. – Reinstate Monica - M. Schröder Sep 25 '16 at 10:18
3

The reason for the behavior you get, is that with every \pause you actually create a new page in the resulting PDF. That means, with every \pause the compiler loops again over your entire frame. So after the first run the counter is at 4, processing the next \pause with a new frame it will continue with 5.

The answer from samcarter is the elegant solution, the following is a quick-and-dirty workaround:

You just need to reset the counter at the beginning of the frame to the value it was before creating the first frame:

 \setcounter{preFrameDefNum}{\value{DefNum}}
 \begin{frame}{test}
 \setcounter{DefNum}{\value{preFrameDefNum}}

You can automate the first command by:

\usepackage{etoolbox}
\AtBeginEnvironment{frame}{\setcounter{preFrameDefNum}{\value{DefNum}}}

Full MWE

\documentclass[serif,envcountsect]{beamer}

\newcounter{DefNum}
\newcounter{preFrameDefNum}
\usecounter{DefNum}
\newcommand{\defi}{\noindent\text{Definition \arabic{DefNum}}\refstepcounter{DefNum}}
\setcounter{DefNum}{1}

\begin{document}

\setcounter{preFrameDefNum}{\value{DefNum}}
\begin{frame}{test} 
\setcounter{DefNum}{\value{preFrameDefNum}}

\defi

\defi

test hello ~\\ \pause
hello test \pause

\begin{itemize}
  \item aa \pause
  \item bb \pause
  \item cc
  \item \defi
\end{itemize}

\defi
\end{frame}
\end{document} 
  • Thanks. Not only provide the solution, but also tell me why the "bad" behavior of pause command. – Kuang Zhou Sep 25 '16 at 11:19
3

If you don't mind, I would like to suggest an alternative approach instead if trying to solve your actual question:

Beamer already has mechanism in place to display numbered definitions etc. Normally they are inside beamer blocks, but if you want them to be inline as in your example, you can simply adjust their definition.

\documentclass{beamer}

\usecolortheme{orchid}

\makeatletter
\setbeamertemplate{theorem begin}{%
    \inserttheoremname
    \inserttheoremnumber
    \space
}

\setbeamertemplate{theorem end}{}
\makeatother


\begin{document}
    \begin{frame}{A theorem in a box}
        \begin{definition}
            bla
        \end{definition}

        \begin{definition}
            bla
        \end{definition}

        test hello ~\\ \pause
        hello test \pause

        \begin{itemize}
           \item aa \pause
           \item bb \pause
           \item cc
           \item \begin{definition}
                    bla
                 \end{definition}
        \end{itemize}

        \begin{definition}
            bla
        \end{definition}
    \end{frame} 
\end{document}

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.