6

TiKZ pics include a new key called name prefix ... TiKZ documentation defines it with

/tikz/name prefix ..

This key is available only inside the code of a pic. There, it (locally) changes the name prefix to the value it had outside the pic. This allows you to access nodes outside the current pic.

but it doesn't show any example. In TeX.SX I've just found JLDiaz answer to Nodes and matrix of nodes where I don't understand what name prefix .. is used for. So, could you write a more simple example of its use?

Ignasi
  • 136,588
  • When your document has these name prefixes, you can for instance draw an arrow between the two pictures, even if you used seperate figure environments for them. – 1010011010 Jun 17 '14 at 13:51
  • @1010011010 I understand what name prefix does within an scope. There are some example in pgfmanual. But not whithin a pic where is key name prefix .. (with .. not just name prefix) defined. – Ignasi Jun 17 '14 at 14:02
  • See http://tex.stackexchange.com/q/176900/15925 – Andrew Swann Jun 17 '14 at 15:01
  • 1
    When the code inside a pic refers to any node name, tikz automatically inserts before it a prefix (which is given where the pic is used). For example, \path (0,0) pic (foo) {bar}; inserts the pic defined by bar/.pic, using foo as prefix. Whenever inside the code of that pic you refer to node (X), tikz indeed interprets (fooX), since the prefix is automatically prepended. name prefix .. avoids that, so allows you to access to nodes defined in the main pic. Read the .. as in the unix commmand cd .. – JLDiaz Jun 23 '14 at 16:51
  • 1
    In my answer you refer to, it was neccessary to add this in the node which used a shadow, because internally tikz refers to the node (current bounding box) for computing the shadow, and without name prefix .. option, an error appears about unknown node (foocurrent bounding box) (being foo the name prefix in this example). – JLDiaz Jun 23 '14 at 16:53

1 Answers1

7

As an extension of the example used in the PGF documentation about pics, I propose to draw coconuts, and attach them to seagulls (as we are lacking swallows).

We will use the seagull pic defined in the manual, and introduce another very simple pic called coconut, but we want to ensure that each coconut is attached to a seagull.

Here is the definition of both pics:

\tikzset{
    seagull/.pic={
        % Code for a "seagull". Do you see it?...
        \coordinate (-left wing) at (-3mm,0);
        \coordinate (-head) at (0,0);
        \coordinate (-right wing) at (3mm,0);

        \draw (-3mm,0) to [bend left] (0,0) to [bend left] (3mm,0);
    },
    coconut/.pic={
        \draw (0,0) circle[radius=2mm];
        \draw[name prefix ..,very thin] (0,2mm) -- (#1-head)  .. controls ++(-2mm,-0.3mm) and ++(0,-2mm) .. (#1-head) .. controls ++(2mm,-1mm) and ++(0,-2mm) .. (#1-head);
    }
}

Notice that we used the key name prefix .. in the draw call for the coconut, and that a parameter is needed to tell the coconut to which seagull it is attached. Let's try to use these pics:

\begin{tikzpicture}
    \pic (Veronique) at (0.5,1) {seagull};
    \pic (Virginie) at (-1,0.7) {seagull};
    \pic (Emma) at (0,0) {seagull};
    \pic (Coco) at (0.1,-1) {coconut=Emma};
    \pic (Nut) at (-1.1,-0.8) {coconut=Virginie};
\end{tikzpicture}

Which gives the result:

Seagulls and coconuts