12

In a TikZ pic, the combination of -- cycle, \coordinates, and the name key (or using parenthesis) only works sometimes.

 \documentclass{article}
 \usepackage{tikz}
 \begin{document}
 \tikzset{
   mypic/.pic = {
     \coordinate (a) at (0,0);
     \coordinate (b) at (1,0);
     \coordinate (c) at (1,1);
     \coordinate (d) at (0,1);
     \path[draw,fill=yellow] (a) -- (b) -- (c) -- (d) -- cycle;
   },
 }
 try 1:        \tikz \pic (k)     {mypic};
 try 2:        \tikz \pic[name=l] {mypic};
 without name: \tikz \pic         {mypic};
 try 3:        \tikz \pic (m)     {mypic};
 \end{document}

Gives:

1st and 2nd square is open, 3rd and 4th square are closed

Dropping the coordinates and using \path[draw,fill=yellow] (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle; works as expected, all squares are closed and filled.

user72575
  • 143
  • 1
    if you run the one without name before the others they all behave in the right way...weird! It seems that if you run it without name once then all the others work correctly – Bordaigorl Mar 15 '17 at 12:04
  • 1
    Welcome! Nice question. I suggest never using the minimal class, which is not intended for making minimal examples. – egreg Mar 15 '17 at 12:15
  • If you replace cycle with the first coord (a), at least you get all four sides drawn, but still without color. – Nicolás Ozimica Mar 15 '17 at 14:34
  • If you put without name at the first line, all four trial will success. – Symbol 1 Mar 15 '17 at 14:52

2 Answers2

5

You can replace

\path[draw,fill=yellow] (a) -- (b) -- (c) -- (d) -- cycle;

by

\path[draw,fill=yellow] (a.center) -- (b.center) -- (c.center) -- (d.center) -- cycle;

Reason

When you try to name the pic (picname), TikZ will name the coordinate (cooname) in the pic by (picnamecorname). This is quite convenient because you can refer to the same coordinate in the different pic.

When TikZ wants to construct the path, it will test whether the given input is a node. If it is a node, it will break the path into pieces so that the path will not overlap the node. On the other hand, if it is a coordinate, it will continue the path

This test is done by

\expandafter\ifx\csname pgf@sh@ns@#2\endcsname\tikz@coordinate@text

where #2 is corname and \tikz@coordinate@text is the string coordinate. This test will fail because \pgf@sh@ns@corname is undefined. Remember that the coordinate is named picnamecorname, so the correct one is \pgf@sh@ns@picnamecorname.

The final result is that the path is broken into pieces and cannot be filled.

However if you execute the unnamed version first, TikZ will draw the expected picture and it will be protocoled. Therefore the drawing will remain the same.

This is probably related to http://tex.stackexchange.com/questions/75146/draw-a-path-between-many-nodes-using-foreach
Symbol 1
  • 36,855
  • Presumably related to the fact that the seagull example in the manual doesn't work, although it used to. (Emma & Alexandra on 254.) ? – cfr Mar 16 '17 at 03:50
  • @cfr I just answered 194362 and it turns out that there are related in the sense that it is hard to tell whether the prefix is added or not. – Symbol 1 Jun 18 '17 at 13:21
  • 194362? Is that another question? (You mention 75146 above, but that's crossed out.) – cfr Jun 18 '17 at 15:40
2

The most simple solution I've come up with, is this:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\tikzset{
  mypic/.pic = {
    code = {
      \coordinate (a1) at (0,0);
      \coordinate (c1) at (1,1);
      \path[draw,fill=yellow] (a1) rectangle (c1);
    }
  }
}

try 1:        \tikz \pic (k)     {mypic};
try 2:        \tikz \pic[name=l] {mypic};
without name: \tikz \pic         {mypic};
try 3:        \tikz \pic (m)     {mypic};

\end{document}

Where instead of defining all 4 coordinates, just define those coords that form the rectangle.

The major problem with this solution is that is only works with rectangles. If you want to draw more specific figures, you should have use other shapes from the shapes module.


Related to the problem you're facing: in page 218 of the manual (version 3.0.1a), it says that even though you don't close the figure (by not writing cycle, or not repeating in the last position the first coordinate), its fill color will be drawn anyway.

According to what I've experimented, this must be a problem with how the coordinates are managed to draw lines between their points.