In the solution to Intersections in PGFplots it was pointed out that each \addplot command was drawn in its own scope and using name path would not survive outside of that \addplot command. The solution was to use name path global so that the path name would be known outside of the particular \addplot that generated that path. This works fine -- However, in a large document it is possible that the same name may be used again.
Am wondering if there is a way to clear the paths that were named via name path global. Even if I needed to do this outside of the current \tikzpicture environment that too would be fine as I would just simple add an empty \tikzpicture that performed this clean up after each picture where it was used. This would be preferable as then I don't need to worry about name conflicts.
I don't see anything about this is the documentation, so if it not currently possible, this might be a good feature to consider for future release.
**** Update (using sol'n provided by Andrew) ****
Attempted to use sol'n, but it seems to only clear the first path.. The following code works fine if the path names are changed in the 2nd graph (and shows 2 intersections), but as is it shows 4 intersections as shown here
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\makeatletter
\tikzset{%
clear global paths/.style={
execute at end picture=\clear@global@paths,
name path global/.append code={
\ifx\global@paths\pgfutil@empty
\gdef\global@paths{##1}%
\else
\xdef\global@paths{\global@paths,##1}%
\fi
}
},
clear global paths now/.code={
\expandafter\global\expandafter\let\csname tikz@intersect@path@name@#1\endcsname=\relax
}
}
\let\global@paths=\pgfutil@empty
\def\clear@global@paths{%
\edef\@temp{\noexpand\pgfkeys{/tikz/clear global paths now/.list={\global@paths}}}%
\@temp
\global\let\global@paths=\pgfutil@empty
}
\makeatother
%--------------------------------------------------
\begin{document}
\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}[clear global paths]
\draw[name path global=GraphCurve, mark=none, domain=-2.5:2.5, thick] plot ({\x},{\x*\x});%
\draw [red, thick, name path global=HorizontalLine] (-2.5,3) -- (2.5,3);%
\ShowIntersection{GraphCurve}{HorizontalLine}
\end{tikzpicture}
\begin{tikzpicture}[clear global paths]
\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,4) (2.5,4)};%
\ShowIntersection{GraphCurve}{HorizontalLine}
\end{axis}
\end{tikzpicture}
\end{document}