Intersections in PGFplots

I can get intersections to work in TikZ, but not PGF plots. I defined a command to show the intersections to make sure that it is identical in both cases. Commenting out the \ShowIntersection line in the 2nd graph results in a syntax error. Is there a different way to name the curves in PGF plots?

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

% Define this as a command to ensure that it is same in both cases
\newcommand*{\ShowIntersection}{
\fill
[name intersections={of=GraphCurve and HorizontalLine, name=i, total=\t}]
[red, opacity=1, every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}{(i-\s) circle (2pt)
node [above left] {\s}};
}

\begin{tikzpicture}
\draw[name path=GraphCurve, mark=none, domain=-2.5:2.5, thick]%
plot ({\x},{\x*\x});%

\draw [red, thick, name path=HorizontalLine]
(-2.5,3) -- (2.5,3);%

\ShowIntersection% Works fine here
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}

({x},{x*x});%

coordinates{(-2.5,3) (2.5,3)};%

%\ShowIntersection% Error: Do not know path "GraphCurve"

\end{axis}
\end{tikzpicture}
\end{document}

-

Each plot is drawn in its own scope, so the named paths won't be known outside the \addplot command. In this case, you should use the name path global option instead of name path:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

\newcommand*{\ShowIntersection}{
\fill
[name intersections={of=GraphCurve and HorizontalLine, name=i, total=\t}]
[red, opacity=1, every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}{(i-\s) circle (2pt)
node [above left] {\s}};
}

\begin{tikzpicture}
\begin{axis}

\addplot[name path global=GraphCurve, mark=none, domain=-2.5:2.5, thick]%
({x},{x*x});%

\addplot [red, thick, name path global=HorizontalLine]
coordinates{(-2.5,3) (2.5,3)};%

\ShowIntersection% Works

\end{axis}
\end{tikzpicture}
\end{document}


Note that name path global creates problems if there are paths with the same name in the document (even if they are in different tikzpicture environments), some intersections will be found several times. I would recommend to assign unique names to the paths, and adapt the \ShowIntersection macro to take the names of the paths as arguments:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

% Define this as a command to ensure that it is same in both cases
\newcommand*{\ShowIntersection}[2]{
\fill
[name intersections={of=#1 and #2, name=i, total=\t}]
[red, opacity=1, every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}{(i-\s) circle (2pt)
node [above left] {\s}};
}

\begin{tikzpicture}
\begin{axis}

\addplot[name path global=a, mark=none, domain=-2.5:2.5, thick]%
({x},{x*x});%

\addplot [red, thick, name path global=b]
coordinates{(-2.5,3) (2.5,3)};%

\ShowIntersection{a}{b}

\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\draw[name path=c, mark=none, domain=-2.5:2.5, thick]%
plot ({\x},{\x*\x});%

\draw [red, thick, name path=d]
(-2.5,3) -- (2.5,3);%

\ShowIntersection{c}{d}
\end{tikzpicture}
\end{document}

-
Thankt Jake, this works great. However, this brings up the question if there is a way to clear these global path? – Peter Grill Jun 23 '11 at 7:08