4

I am trying to create a plot where the points would be added sequentially over multiple slides. But the added code produces a bunch of Undefined control sequence. errors.

Interestingly, when I use \x and \xi as the variable and the index variable respectively in the for loop, the code starts running and I get a huge 250+ page PDF! It's filled with empty plots except the very last page where the plot has all the points.

MWE:

\documentclass{beamer}

\usepackage{tikz} \usetikzlibrary{overlay-beamer-styles}

\usepackage{pgfplots} \pgfplotsset{compat = 1.18}

\begin{document} \begin{frame}{Frame name} \begin{figure} \centering \begin{tikzpicture} \begin{axis} \foreach \point [count = \pointindex] in {-2, 5, -1} { \addplot [ only marks, mark = *, samples at = {\point}, visible on = <\pointindex->, ] {x^2}; } \end{axis} \end{tikzpicture} \end{figure} \end{frame} \end{document}

Saptam
  • 127

1 Answers1

3

The problem is, that \pointindex is defined locally to the \foreach loop. But for overlays you need global values. You can use something like:

\documentclass{beamer}

\usepackage{tikz} \usetikzlibrary{overlay-beamer-styles}

\usepackage{pgfplots} \pgfplotsset{compat = 1.18}

\begin{document} \begin{frame}{Frame name} \begin{figure} \centering \begin{tikzpicture} \begin{axis} \foreach \point [count = \pointindex] in {-2, 5, -1} { \xdef\ADDPLOT{% \noexpand\addplot[visible on = {<\pointindex->}, only marks, mark = *, samples at = {\point} ] } \ADDPLOT {x^2}; } \end{axis} \end{tikzpicture} \end{figure} \end{frame} \end{document}

three frames

cabohah
  • 11,455
  • Neat solution. Any idea why changing the macro names makes it go haywire? After all, those macro names are precisely what the PGF manual uses (the section on package pgffor). – Saptam Dec 07 '22 at 07:49
  • @Saptam \xi is already defined (\mathchar"118) outside the loop and can be interpreted by visbile on. Depending on the outside definition of the index variable macro you would get different result. Sometimes with, sometimes without error message. But always more or less an accident. – cabohah Dec 07 '22 at 08:15