3

I am trying to understand the naming behavior of paths inside a pic definition. Consider the following

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\tikzset{%
    path1/.pic={%
        \coordinate (-p) at (-0.5,-0.5);
        \draw[red, name path=-horz-fails] (-1,0.5) to (1,0.5);
        \draw[black, name path=-vert] (0,-1) to (0,1);
    };
}
\tikzset{%
    path2/.pic={%
        \draw[blue, name path=-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}

In this example, all four lines are correctly drawn. Further, the uncommented intersection is correctly drawn; however, if I uncomment the second intersection, I get get the compiler error

! Package tikz Error: I do not know the path named `-horz-works'. Perhaps you 
 misspelt it.

From my searches, I came across this question and the associated bug report from the answer. As with that answer, the last path named in the pic is available outside the definition. I thought this might be related, but I cloned the repository and tried compiling with the fix, but I still get the same result. I also tried to use name path .., but that did not make the path available in the main picture environment.

My question is: Is this a manifestation of the same bug? If not, what are the naming rules for paths inside a pic definition?

I notice that the coordinate (-p here) inherits the pic name outside scope of the pic as I expected and I would like to reference the path in the same manner.


Motivation

My ultimate goal is to draw a line to the intersection of two paths defined inside a generated pic. I have some Python code that generates the lines of latitude and longitude along a sphere. I would like to use the pic multiple times within a single picture; thus, I would like to refer to the paths with the parent pic name to compute only those intersections that will further what I am trying to show. Since I am computing the paths, I could define the coordinates of the intersections, but I would like to let TikZ do the job and that seems like overkill to me.

2

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}

enter image description here

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)

  • I forgot to mention I tried name path global. Good call. My problem with that solution is the name collision because I want to use the pic multiple times in a single picture. Any thoughts on how to get around that? – Keith Prussing Mar 6 at 6:25
  • @KeithPrussing I added a way to make the path names unique, they get prepended by the name of the pic, just like the coordinates get. – user121799 Mar 6 at 6:36
  • Thanks. This solves the immediate problem, and I guess I'll just have to dig into the intersection library to get more on the naming. – Keith Prussing Mar 8 at 4:08
1

Here a quick hack by simply put every path in a single pic.

This does not answer your question but ensures that you no longer have any mistakes.

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

\tikzset{%
    path1/.pic={%
        \coordinate (-p) at (-0.5,-0.5);
        \draw[red, name path=-horz-fails] (-1,0.5) to (1,0.5);
       % \draw[black, name path=-vert] (0,-1) to (0,1);
    },
        path3/.pic={%
        \coordinate (-p) at (-0.5,-0.5);
        %\draw[red, name path=-horz-fails] (-1,0.5) to (1,0.5);
        \draw[black, name path=-vert] (0,-1) to (0,1);
    },
    path2/.pic={%
        \draw[blue, name path=-horz] (-1,0) to (1,0);
    };
}

\begin{document}
\begin{tikzpicture}
    \pic [name=p1] at (0, 0) {path1};
    \pic (p2) at (0, 0) {path2};
    \pic (p3) at (0,0) {path3};
    \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}

screenshot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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