# In tikz, how to pass named coordinates to a named pic?

I have found problems when passing a named argument to a pic, but only when the pic is also named.

Follows the illustrative code and the problem occurs in the line tagged with %NOT OK. That line gives an error because a name to the pic is given (MyLink 3); but if the pic name is omitted (line tagged %OK 2) the code works as expected. On the other hand, keeping the pic named, if using the explicit value for the coordinate (instead of name P1 as shown), the code works without errors (line tagged %OK 1).

Just a remark on the output that may give a clue to the solution but that is escaping me: if the coordinates are passed explicitly, they are considered in a local reference frame of the pic; if, on the other hand, they are passed as a defined name (after the \coordinate command) they are considered in the global reference frame of the entire tikzpicture!

Any help for this problem (or a workaround) is welcome. Thanks in advance.

\documentclass[]{standalone}
\usepackage{tikz}
\tikzset{
code={
\coordinate (-start) at (0,0);
\coordinate (-end)   at (#1);
\draw [thick] (-start) -- (-end);
\draw [fill=white] (-start) circle (3pt);
\draw [fill=white] (-end)   circle (3pt);
}
}
}
\begin{document}
\begin{tikzpicture}
\draw[step=5pt,help lines] grid (40pt,40pt);

\coordinate (P1) at (-10:25pt);

\end{tikzpicture}

\end{document}


• The commented line works fine with the latest version of TikZ (3.1.5b). – Torbjørn T. Sep 14 '20 at 22:06
• Thanks a lot. Indeed, I tested in version 3.0.1a. I need to upgrade my version! – vsantos Sep 15 '20 at 9:56

[This is really a long comment...that is too long for a comment]

Like Torbjørn, the code in the OP compiles for me so I wonder if the problem is really that the OP does not seem to be using the correct syntax for the \pic command. According to the manual, the syntax is:

\path ... pic ⟨foreach statements⟩ [⟨options⟩] (⟨prefix⟩) at(⟨coordinate⟩) :⟨animation attribute⟩={⟨options⟩} {⟨pic type⟩} ...;

In particular, when it is supplied the prefix should come before the coordinates. As such the MWE should look more like:

\documentclass[border=2mm,tikz]{standalone}
\tikzset{
code={
\coordinate (-start) at (0,0);
\coordinate (-end)   at (#1);
\draw [thick] (-start) -- (-end);
\draw [fill=white] (-start) circle (3pt);
\draw [fill=white] (-end)   circle (3pt);
}
}
}
\begin{document}

\begin{tikzpicture}
\draw[step=5pt,help lines] grid (40pt,40pt);

\coordinate (P1) at (-10:25pt);

\end{tikzpicture}

\end{document}


I have changed the the third pic to use MyLink 2. With the code in the OP, the red link is just drawn over the top of the green link.

This produces:

What is actually happening here is that:

• the first (black) pick draws a black link from the origin to the point (60:30pt)
• the second pic draws a blue link from the end of the black link to the relative coordinate that is -10 degrees and 25pt away
• the third pic draws a green link from the end of the blue link to the absolute coordinate P1.
• the fourth pic draws a link from the end of the first black link to the absolute coordinate P1
• Thanks for the complete answer. Is still fails on my setup, but I believe that the problem is due to my older Tikz version, as Torbjørn stated in the comment above. – vsantos Sep 15 '20 at 10:02