3

I am trying to use a counter in the tikzpicture environment. The counter needs to keep track of the slide from when the picture needs to be shown; that way, it is easy to add or delete a line.

    \documentclass{beamer}

    \usepackage{tikz}

    \begin{document}

    \newcounter{test}
    \addtocounter{test}{1}
    \begin{frame}
    \begin{tikzpicture}
    \onslide<\arabic{test}->{\draw (0,0)--(1,1);}
    \addtocountour{test}{1};
    \onslide<\arabic{test}->{\draw (1,1)--(2,0);}
    \addtocounter{test}{1};
    \onslide<\arabic{test}->{\draw (2,0)--(3,1);}
    \end{tikzpicture}
    \end{frame}

    \end{document}
jub0bs
  • 58,916
Thomas
  • 157

1 Answers1

2

A few problems with your code: you misspelled the first \addtocounter and wrote \addtocountour instead; also, as discussed in Andrew Stacey's post, if you want to use the value of a counter inside overlay specifications such as \onslide<...>, you should use \value (not \arabic, like you did).

However, it seems that using \onslide<\value{...}> within a tikzpicture environment generates errors. I'm not sure why; other contributors will probably chime in and provide a more complete answer than mine is.

In the meantime, here's a workaround; it uses PGF integers (defined with the \pgfmathtruncatemacro command) instead of counters but produces the expected output:

enter image description here

\documentclass{beamer}

\usepackage{tikz}

\begin{document}

\pgfmathtruncatemacro\test{1}
\begin{frame}
    \begin{tikzpicture}
    \onslide<\test->{\draw (0,0)--(1,1);}
    \pgfmathtruncatemacro\test{\test+1}
    \onslide<\test->{\draw (1,1)--(2,0);}
    \pgfmathtruncatemacro\test{\test+1}
    \onslide<\test->{\draw (2,0)--(3,1);}
    \end{tikzpicture}
\end{frame}

\end{document}
jub0bs
  • 58,916