Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.