# How to save the intersection points as nodes for multiple use later?

The following MWE will not compile because \n is not recognized in \foreach.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\draw [red, smooth, name path=first] plot coordinates {(1,1)(2,3)(4,6)(5,2)(7,5)};
\draw [blue, smooth, name path=second] plot coordinates {(1,5)(2,1)(4,3)(5,5)(7,1)};
\path [name intersections={of=first and second, total=\n}];
\foreach \i in {1,...,\n}{\draw (intersection-\i) circle [radius=3pt];}
\end{tikzpicture}
\end{document}


My objective is to save the intersection points as nodes for multiple use later. How to do this?

I think the problem is that the \n command is only defined within the scope of the current TikZ command (i.e. within the scope of the \path command in your example). The following fix combines your \path and \foreach commands into a single \draw command. It is very similar to an example given in 13.3.2 Intersections of Arbitrary Paths of the TikZ manual.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\draw [red, smooth, name path=first] plot coordinates {(1,1)(2,3)(4,6)(5,2)(7,5)};
\draw [blue, smooth, name path=second] plot coordinates {(1,5)(2,1)(4,3)(5,5)(7,1)};
\draw [name intersections={of=first and second, total=\n}]
\foreach \i in {1,...,\n}{(intersection-\i) circle [radius=3pt]};
\end{tikzpicture}
\end{document}


Update 1. I understand now that you wish to save the nodes outside of the scope of a single command. Following Andrew's suggestion in the comments below, you can define the nodes using the \coordinate command, like this.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\draw [red, smooth, name path=first] plot coordinates {(1,1)(2,3)(4,6)(5,2)(7,5)};
\draw [blue, smooth, name path=second] plot coordinates {(1,5)(2,1)(4,3)(5,5)(7,1)};
\draw [name intersections={of=first and second, total=\n}]
\foreach \i in {1,...,\n} {(intersection-\i) coordinate (red-blue-intersection-\i)};
\end{tikzpicture}
\end{document}


Update 2. You may also wish for \n, the number of nodes, to escape the scope. Then those three \draw commands in my code above can be replaced by a loop. I don't know a 'good' way to do this, but here's a hacky way that seems to work.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\draw [red, smooth, name path=first] plot coordinates {(1,1)(2,3)(4,6)(5,2)(7,5)};
\draw [blue, smooth, name path=second] plot coordinates {(1,5)(2,1)(4,3)(5,5)(7,1)};
\draw [execute at begin node={\global\let\n=\n}, name intersections={of=first and second, total=\n}]
\foreach \i in {1,...,\n} {(intersection-\i) coordinate (red-blue-intersection-\i)};
\foreach \i in {1,...,\n}

• @mozartstraße Replace (intersection-\i) circle [radius=3pt] by (intersection-\i) coordinate (red-blue-intersection-\i) or whatever naming scheme you want. – Loop Space Jul 3 '13 at 10:29
• It's not ideal, but you can just put /utils/exec={\global\let\n=\pgfintersectionsolutions} (or /utils/exec={\global\let\n=\n}) after the name intersections key to get the same effect as update 2. Or put the global assignment inside \pgfextra{} after all of the options. – Mark Wibrow Jul 3 '13 at 13:48