5
\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.

  • 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. – user121799 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. – user121799 Apr 14 '18 at 17:09
4
\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.

  • 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
5

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.

  • "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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.