7
\documentclass[tikz,border=2pt]{standalone}

\begin{document}

\begin{tikzpicture}
   \node at (0,0) (A) {A};
   \node at (0,1) (B) {B};
   \node at (1,0) (C) {C};
   \draw (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}

\begin{tikzpicture}
   \draw (0,0) node (A) {A} -- 
         (0,1) node (B) {B} -- 
         (1,0) node (C) {C} -- 
         cycle ;
\end{tikzpicture}

\end{document}

Some of the subtitles of TikZ are a little confusing. The example above produces the following pictures

cycle1 cycle2

In the first case, the lines don't enter the node labels, but the cycle command isn't activated. In the second, the lines enter the node labels, but the cycle command is activated. I naively thought the two pictures should be the same. Why are the pictures different? Why do the lines behave differently? Why does cycle behave differently?

There is a similar question here.

Cycle option when drawing between nodes in TikZ

in which the answer explains that "bare nodes" do not have zero size. But it doesn't explain why the nodes are treated differently in these two cases. The phrase "bare node" does not appear in the TikZ Manual.

pheon
  • 786
  • I do not understand why you are saying that this is not explained in this answer. If you read it carefully, you'll see that it does explain why cycle does not work. –  Apr 14 '18 at 17:02
  • @marmot. Explanations are in the eye of the beholder. That answer does partial work, but is not complete. See below. – pheon Apr 14 '18 at 17:06
  • I do honestly not understand what is new in the answer even though I highly respect @percusse and lear a lot from his posts and answers. –  Apr 14 '18 at 17:09

2 Answers2

6

By bare nodes node names are meant. When you use the node name, TikZ tries to be smart and checks where the target is and calculates the point on its shape border. Same goes for the target.

So essentially you are creating 3 separate paths and the last one is a line hence when used with cycle you get the last line printed twice. Because cycle only draws back to the last continuous path starting point.

If you use actual coordinates instead of node names such as (A.center) it works again.

percusse
  • 157,807
  • "By bare nodes node names are meant." But both pictures have named nodes. But they are different. – pheon Apr 14 '18 at 16:57
  • @pheon No not really. You are using coordinates to denote the points and placing nodes along the way without breaking the path. That is actually a very nice feature of TikZ syntax. – percusse Apr 14 '18 at 16:59
  • So \draw (0,0) -- (0,1); makes two coordinates (which I thought were a special type of node.) But \draw (0,0) node (A) {} -- (0,1); makes two coordinates plus a node with a name at the same place as the first coordinate? – pheon Apr 14 '18 at 17:03
  • @pheon Yes exactly. Nodes are not coordinates. They are shape objects placed at the coordinates either given by at (x,y) or the last given coordinate on the path. But coordinate is a node shape. By coordinate here I mean standard points with x and y component. – percusse Apr 14 '18 at 17:04
  • You might get mad at me, but I really think that this very nicely explained in this answer. What am I missing? –  Apr 14 '18 at 17:06
  • 1
    In the first case, the third path starts at node C (C.center actually, because there is no direction, see below) with a moveto operator. Then cycle uses the closepath operator that closes the path to the starting point. Thus, the -- cycle can be translated to (C.center) -- (C.center), not a very long line. – Heiko Oberdiek Apr 14 '18 at 17:07
  • "Nodes are not coordinates". But are not coordinates nodes? – pheon Apr 14 '18 at 17:08
  • Would \draw (A) -- (B) -- (C); be three paths but \draw (A.center) -- (B .center) -- (C.center); be one path? That would be subtle. – pheon Apr 14 '18 at 17:13
  • @pheon There is a terminology clash. Coordinates in TikZ are (x,y). There is also a coordinate node shape. Just like \node[circle] you can make a coordinate node via \node[coordinate] which is a special type of node. TikZ also has a special shortcut for this shape and it is coordinate just like node. Basic need for such a shape is that you cannot insert a named coordinate (4,3) but you can insert (4,3) coordinate (a) on a path to refer to it later. – percusse Apr 14 '18 at 17:13
  • @pheon That would be subtle. When you read my last comment it's gonna be clearer I hope. – percusse Apr 14 '18 at 17:15
  • I think I get it. With a coordinate node type with a name, you can use the name of the coordinate to refer to the coordinate of that node and the latter coordinate is not a node. – pheon Apr 14 '18 at 17:23
5
\begin{tikzpicture}
   \node at (0,0) (A) {A};
   \node at (0,1) (B) {B};
   \node at (1,0) (C) {C};
   \draw (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}

In this case you are placing some nodes centered on certain coordinates. Every node is, by default, a rectangle around its contents. It has some inner sep between its contents and border. These borders are not drawn by default, but they exist.

When you write \draw (A) -- (B) -- (C) -- cycle;, Tikz tries to join node's centers but stopping lines on nodes' borders. Therefore you see two disjoint lines, one between A and B and another from B to C, but as it's not possible to close the cycle, because you don't have a continous path, there is no line between C and A.

\begin{tikzpicture}
   \draw (0,0) node (A) {A} -- 
         (0,1) node (B) {B} -- 
         (1,0) node (C) {C} -- 
         cycle ;
\end{tikzpicture}

In this case as percusse already explained, your are drawing a closed path between certain coordinates

   \draw (0,0) -- (0,1) -- (1,0) -- cycle ;

and supperposing node on it. It's a different command which produces a different solution.

Ignasi
  • 136,588
  • The key, I think, is that the two \draws do quite different things, despite having an almost identical form. The first draws three separate paths through three existing nodes. The second draws one path and puts three nodes on that path. – pheon Apr 14 '18 at 22:55