4

I have a very complex Tikz figure that is uncovered piece by piece during my presentation. While tweaking the presentation I decided to change the order of things being uncovered and due to the large amount of \onslide commands it is a pain to get it right. In order to help the situation I tried to create the following two macros:

\newcommand*{\BeamerCounter}[1]{%
  \@ifundefined{c@#1}{%
    \newcounter{#1}}{}}

\newcommand*{\BeamerNext}[2]{%
  \@ifundefined{#1#2}{%
    \stepcounter{#1}   
    \newcommand*{#1#2}{\the#1}}{}}

The idea is that at the top level of my document I would do something like:

\BeamerCounter{Fig}
\BeamerNext{Fig}{Part1}
\BeamerNext{Fig}{Part2}

So that in my figure I can use:

\onslide<\FigPart1>{ .. }
\onslide<\FigPart2>{ .. }
\onslide<\FigPart1, \FigPart2>{ .. }

Minimal (non)working example:

\documentclass{beamer}

\newcommand*{\BeamerCounter}[1]{%
  \@ifundefined{c@#1}{%
    \newcounter{#1}}{}}

\newcommand*{\BeamerNext}[2]{%
  \@ifundefined{#1#2}{%
    \stepcounter{#1}
    \newcommand*{#1#2}{\the#1}}{}}

\BeamerCounter{Fig}
\BeamerNext{Fig}{Part1}
\BeamerNext{Fig}{Part2}

\begin{document}
\begin{frame}
  \begin{itemize}
    \item<FigPart1-> item1
    \item<FigPart2-> item2
  \end{itemize}
\end{frame}
\end{document}

Which yields the following errors:

t.tex:12: You can't use `\spacefactor' in vertical mode.
t.tex:12: leading text: \BeamerCounter{Fig}
t.tex:12: Missing $ inserted.
t.tex:12: leading text: \BeamerCounter{Fig}
t.tex:12: Missing \begin{document}.
t.tex:12: leading text: \BeamerCounter{Fig}
t.tex:13: You can't use `\spacefactor' in math mode.
t.tex:13: leading text: \BeamerNext{Fig}{Part1}
t.tex:13: Missing number, treated as zero.
t.tex:13: leading text: \BeamerNext{Fig}{Part1}
t.tex:13: Missing control sequence inserted.
t.tex:13: leading text: \BeamerNext{Fig}{Part1}
t.tex:13: You already have nine parameters.
t.tex:13: leading text: \BeamerNext{Fig}{Part1}
t.tex:13: You can't use `the letter F' after \the.
t.tex:13: leading text: \BeamerNext{Fig}{Part1}
t.tex:14: You can't use `\spacefactor' in math mode.
t.tex:14: leading text: \BeamerNext{Fig}{Part2}
t.tex:14: Missing number, treated as zero.
t.tex:14: leading text: \BeamerNext{Fig}{Part2}
t.tex:14: Missing control sequence inserted.
t.tex:14: leading text: \BeamerNext{Fig}{Part2}
t.tex:14: You already have nine parameters.
t.tex:14: leading text: \BeamerNext{Fig}{Part2}
t.tex:14: You can't use `the letter F' after \the.
t.tex:14: leading text: \BeamerNext{Fig}{Part2}
t.tex:15: Missing $ inserted.
1

1 Answer 1

8

There are several weaknesses in your code.

  1. You want to use the macro \@ifundefined, so the code must be surrounded by \makeatletter and \makeatother as explained in What do \makeatletter and \makeatother do?

  2. \newcommand*{#1#2}{\the#1} is wrong for several reasons; suppose you do \BeamerNext{Fig}{Part1}; then TeX would try

    \newcommand*{FigPart1}{\the Fig}
    
  3. Of course \newcommand*{FigPart1} is illegal, but you can't solve this by saying \newcommand*{\#1#2} in the definition, because this would be illegal as well: \# is the name of a token.

  4. Also \the#1 wouldn't refer to \theFig, because \the is already a token.

  5. You don't want \theFig anyway, but the current value of \theFig.

  6. Even if this worked, \item<FigPart2-> wouldn't do what you expect, because FigPart2 is not the name of a command.

Here's an amended version.

\documentclass{beamer}

\makeatletter
\newcommand*{\BeamerCounter}[1]{%
  \@ifundefined{c@#1}{%
    \newcounter{#1}}{}}

\newcommand*{\BeamerNext}[2]{%
  \@ifundefined{#1#2}{%
    \stepcounter{#1}%
    % \edef to get the current value
    \expandafter\edef\csname#1#2\endcsname{\the\value{#1}}}{}%
}
\newcommand{\BC}[1]{\@nameuse{#1}} % for using the newly defined command
\makeatother

\BeamerCounter{Fig}
\BeamerNext{Fig}{Part1}
\BeamerNext{Fig}{Part2}


\begin{document}
\begin{frame}
  \begin{itemize}
    \item<\BC{FigPart1}-> item1
    \item<\BC{FigPart2}-> item2
  \end{itemize}
\end{frame}
\end{document}

enter image description here

2
  • Thank you! This made my life so much easier! And thank you for explaining what was wrong with the way I was trying to do it.
    – David Holm
    Jan 30, 2014 at 13:59
  • @DavidHolm You're welcome!
    – egreg
    Jan 30, 2014 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .