TransWikia.com

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

TeX - LaTeX Asked by vsantos on August 14, 2021

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{
pics/link/.style={
   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);

pic                           (MyLink 1) {link={60:30pt}};
pic [blue]  at (MyLink 1-end) (MyLink 2) {link={-10:25pt}}; %OK 1
pic [green] at (MyLink 1-end)            {link={P1}};       %OK 2
%pic [red]   at (MyLink 1-end) (MyLink 3) {link={P1}};      %NOT OK!

end{tikzpicture}

end{document}

Output of the given code

One Answer

[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{
  pics/link/.style={
     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);

    pic         (MyLink 1)                   {link={60:30pt}};
    pic [blue]  (MyLink 2) at (MyLink 1-end) {link={-10:25pt}}; %OK 1
    pic [green]            at (MyLink 2-end) {link={P1}};       %OK 2
    pic [red]   (MyLink 3) at (MyLink 1-end) {link={P1}};       %OK!
  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:

enter image description here

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

Answered by user30471 on August 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP