Named paths inside a tikz/pic
Use name path global
, which is made for this: "broadcast" paths outside environments such as scopes or pics.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\tikzset{%
path1/.pic={%
\coordinate (-p) at (-0.5,-0.5);
\draw[red, name path global=-horz-fails] (-1,0.5) to (1,0.5);
\draw[black, name path global=-vert] (0,-1) to (0,1);
};
}
\tikzset{%
path2/.pic={%
\draw[blue, name path global=-horz] (-1,0) to (1,0);
};
}
\begin{document}
\begin{tikzpicture}
\pic [name=p1] at (0, 0) {path1};
\pic (p2) at (0, 0) {path2};
\draw [name intersections={of=-vert and -horz}]
(intersection-1) circle (2pt) to (p1-p);
\draw [name intersections={of=-vert and -horz-fails}]
(intersection-1) circle (2pt);
\end{tikzpicture}
\end{document}
If you want to have unique names, just prepend the names of the paths by the name of the pic
.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\makeatletter
\newcommand{\figname}[1]{\edef#1{\tikz@fig@name}}
\makeatother
\tikzset{%
path1/.pic={%
\figname{\myname}
\coordinate (-p) at (-0.5,-0.5);
\draw[red, name path global=\myname-horz-fails] (-1,0.5) to (1,0.5);
\draw[black, name path global=\myname-vert] (0,-1) to (0,1);
};
}
\tikzset{%
path2/.pic={%
\figname{\myname}
\draw[blue, name path global=\myname-horz] (-1,0) to (1,0);
};
}
\begin{document}
\begin{tikzpicture}
\pic (p1) at (0, 0) {path1};
\pic (p2) at (0, 0) {path2};
\draw [name intersections={of=p1-vert and p2-horz}]
(intersection-1) circle (2pt) to (p1-p);
\draw [name intersections={of=p1-vert and p1-horz-fails}]
(intersection-1) circle (2pt);
\end{tikzpicture}
\end{document}
(same output)