4

I am trying to mark two inflection points using a foreach loop in a pgfplot, but it isn't working.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\infx}{1/sqrt(2)}
  \pgfmathsetmacro{\infy}{exp(-.5)}
  \begin{axis}[
    xmin = -3,
    xmax = 3,
    ymin = -0.1,
    ymax = 1.05,
    domain = -4:4
    ]
    \addplot[blue, samples = 500, smooth] gnuplot {exp(-x^2)};

    \foreach \x/\y in {\infx/\infy, -\infx/\infy}{
      \draw[red] (axis cs: \x, \y) circle[radius = .025];
    }
  \end{axis}
\end{tikzpicture}
\end{document}

I am being told:

Undefined control sequence.^M
<argument> axis cs: \x ^M
                       , \y ^M
l.34   \end{axis}^M
dustin
  • 18,617
  • 23
  • 99
  • 204

1 Answers1

5

The variables need to be expanded since pgfplots don't draw a path immediately but instead collects them. When collecting, it only sees \x and \y and collects them. When it comes to draw them \x and \y are not defined hence you get an error.

If you had one variable, you could use \pgfplotsinvokeforeach but since you have two indices you need to expand them yourself.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\infx}{1/sqrt(2)}
  \pgfmathsetmacro{\infy}{exp(-.5)}
  \begin{axis}[
    xmin = -3,
    xmax = 3,
    ymin = -0.1,
    ymax = 1.05,
    domain = -4:4
    ]
    \addplot[blue, samples = 500, smooth] gnuplot {exp(-x^2)};

    \foreach \x/\y in {\infx/\infy, -\infx/\infy}{
      \begingroup\edef\temp{%
           \endgroup\noexpand\draw[red] (axis cs: \x, \y) circle[radius = .025];}\temp
    }
  \end{axis}
\end{tikzpicture}
\end{document}
percusse
  • 157,807