In the TikZ manual, I found the following command (simplified):

\draw (0,0) -- node {text} (0,1);

The effect of this is that a line is drawn from (0,0) to (0,1), and a text node is positioned halfway along the line:

enter image description here

I don't understand the syntax of this command:

According to the manual (14.2.1), the line-to path operation -- takes a "coordinate or cycle" as an argument. But node is (among other things) a path operation in its own right (17.2.1), which would mean that here we have two path operations in a row without coordinates in between.

I guess node could also be seen as standing in for a coordinate, but only if one is given via at, which is not the case here. And anyway, in that case the line would only go to that specified coordinate.

Effectively node acts as a sort of option to --, but without the standard [] syntax.

Can someone shed light onto this?

  • 1
    While the default is the midpoint, one can also use things like [start] [end] [near start] etc., or more generally [pos=0.5]. If you place the node after the end point as in (A) -- (B) node {text} the default is [end], but you can still use the other options. – John Kormylo Jul 1 at 22:33
  • I found this useful \draw (0,0) -- node[auto]{text} (0,1); Try this with curves. For instance \draw (0,0) to[bend left] node[auto] {text} (0,1); – Symbol 1 Jul 1 at 22:48

Alright, found it. According to the manual section 17.9:

The syntax of the line-to path operation is actually -- [node<node specification>] <coordinate>. (It is even possible to give multiple nodes in this way.) When the optional node is encountered, that is, when the -- is directly followed by node, then the specification(s) are read and “stored away”. Then, after the coordinate has finally been reached, they are inserted again, but with the pos option set.

The "storing away" has limitations, therefore it might be better in general to use the syntax

\draw (0,0) -- (0,1) node[pos=0.5] {text};
| improve this answer | |

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.