11 Nodes

11.1 Nodes and Their Shapes

TikZ offers an easy way of adding so-called nodes to your pictures. In the simplest case, a node is just some text that is placed at some coordinate. However, a node can also have a border drawn around it or have a more complex background and foreground. Indeed, some nodes do not have a text at all, but consist solely of the background. You can name nodes so that you can reference their coordinates later in the picture. However, nodes cannot be referenced across different pictures.

There are no special TEX commands for adding a node to a picture; rather, there is path operation called node for this. Nodes are created whenever TikZ encounters node or coordinate at a point on a path where it would expect a normal path operation (like -- (1,1) or sin (1,1)). It is also possible to give node specifications inside certain path operations as explained later.

The node operation is typically followed by some options, which apply only to the node. Then, you can optionally name the node by providing a name in round braces. Lastly, for the node operation you must provide some label text for the node in curly braces, while for the coordinate operation you may not. The node is placed at the current position of the path after the path has been drawn. Thus, all nodes are drawn “on top” of the path and retained until the path is complete. If there are several nodes on a path, they are drawn on top of the path in the order they are encountered.

 

SVG-Viewer needed.

 

\tikz \fill[fill=examplefill]
     (0,0) node {first node}
  -- (1,1) node {second node}
  -- (0,2) node {third node};

The syntax for specifying nodes is the following:

\path  ... node[<options>](<name>)at(<coordinate>){<text>} ...;

The effect of at is to place the node at the coordinate given after at and not, as would normally be the case, at the last position. The at syntax is not available when a node is given inside a path operation (it would not make any sense, there).

The (<name>) is a name for later reference and it is optional. You may also add the option name=<name> to the <option> list; it has the same effect.

The <options> is an optional list of options that apply only to the node and have no effect outside. The other way round, most “outside” options also apply to the node, but not all. For example, the “outside” rotation does not apply to nodes (unless some special options are used, sigh). Also, the outside path action, like draw or fill, never applies to the node and must be given in the node (unless some special other options are used, deep sigh).

As mentioned before, we can add a border and even a background to a node:

 

SVG-Viewer needed.

 

\tikz \fill[fill=examplefill]
      (0,0) node {first node}
   -- (1,1) node[draw] {second node}
   -- (0,2) node[fill=red!20,draw,double,rounded corners] {third node};

The “border” is actually just a special case of a much more general mechanism. Each node has a certain shape which, by default, is a rectangle. However, we can also ask TikZ to use a circle shape instead or an ellipse shape (you have to include pgflibraryshapes for the latter shape):

 

SVG-Viewer needed.

 

\tikz \fill[fill=examplefill]
      (0,0) node{first node}
   -- (1,1) node[ellipse,draw] {second node}
   -- (0,2) node[circle,fill=red!20] {third node};

In the future, there might be much more complicated shapes available such as, say, a shape for a resistor or a shape for a state of a finite automaton or a shape for a UML class. Unfortunately, creating new shapes is a bit tricky and makes it necessary to use the basic layer directly. Life is hard.

To select the shape of a node, the following option is used:

The following styles influences how nodes are rendered:

The is a special syntax for specifying “light-weighed” nodes:

\path  ... coordinate[<options>](<name>)at(<coordinate>) ...;

This has the same effect as

node[shape=coordinate][<options>](<name>)at(<coordinate>){},

where the at part might be missing.

11.2 Multi-Part Nodes

Most nodes just have a single simple text label. However, nodes of a more complicated shapes might be made up from several node parts. For example, in automata theory a so-called Moore state has a state name, drawn in the upper part of the state circle, and an output text, drawn in the lower part of the state circle. These two parts are quite independent. Similarly, a UML class shape would have a name part, a method part, and an attributes part. Different molecule shape might use parts for the different atoms to be drawn at the different positions, and so on.

Both PGF and TikZ support such multipart nodes. On the lower level, PGF provides a system for specifying that a shape consists of several parts. On the TikZ level, you specify the different node parts by using the following command:

\nodepart{<part name>}

This command can only be used inside the <text> argument of a node path operation. It works a little bit like a \part command in LATEX. It will stop the typesetting of whatever node part was typeset until now and then start putting all following text into the node part named <part name>--until another \partname is encountered or until the node <text> ends.

 

SVG-Viewer needed.

 

\begin{tikzpicture}
  \node [state with output,draw,double,fill=red!20]
  {
    % No \nodepart has been used, yet. So, the following is put in the
    % ``text'' node part by default.
    $q_1$
    \nodepart{output} % Ok, end ``text'' part, start ``output'' part
    $00$
  }; % output part ended.
\end{tikzpicture}

You will have to lookup which parts are defined by a shape.

The following styles influences node parts:

11.3 Options for the Text in Nodes

The simplest option for the text in nodes is its color. Normally, this color is just the last color installed using color=, possibly inherited from another scope. However, it is possible to specificly set the color used for text using the following option:

Just like the color itself, you may also wish to set the opacity of the text only. For this, use the following option:

Next, you may wish to adjust the font used for the text. Use the following option for this:

Normally, when a node is typeset, all the text you give in the braces is but in one long line (in an \hbox, to be precise) and the node will become as wide as necessary.

You can change this behaviour using the following options. They allow you to limit the width of a node (naturally, at the expense of its height).

11.4 Placing Nodes Using Anchors

When you place a node at some coordinate, the node is centered on this coordinate by default. This is often undesirable and it would be better to have the node to the right or above the actual coordinate.

PGF uses a so-called anchoring mechanism to give you a very fine control over the placement. The idea is simple: Imaging a node of rectangular shape of a certain size. PGF defines numerous anchor positions in the shape. For example to upper right corner is called, well, not “upper right anchor,” but the north east anchor of the shape. The center of the shape has an anchor called center on top of it, and so on. Here are some examples (a complete list is given in Section 11.8).

SVG-Viewer needed.

Now, when you place a node at a certain coordinate, you can ask TikZ to place the node shifted around in such a way that a certain anchor is at the coordinate. In the following example, we ask TikZ to shift the first node such that its north east anchor is at coordinate (0,0) and that the west anchor of the second node is at coordinate (1,1).

SVG-Viewer needed.


\tikz \draw           (0,0) node[anchor=north east] {first node}
            rectangle (1,1) node[anchor=west] {second node};

Since the default anchor is center, the default behaviour is to shift the node in such a way that it is centered on the current position.

Unfortunately, while perfectly logical, it is often rather counter-intuitive that in order to place a node above a given point, you need to specify the south anchor. For this reason, there are some useful options that allow you to select the standard anchors more intuitively:

11.5 Transformations

It is possible to transform nodes, but, by default, transformations do not apply to nodes. The reason is that you usually do not want your text to be scaled or rotated even if the main graphic is transformed. Scaling text is evil, rotating slightly less so.

However, sometimes you do wish to transform a node, for example, it certainly sometimes makes sense to rotate a node by 90 degrees. There are two ways in which you can achieve this:

  1. You can use the following option:
  2. You can give transformation option inside the option list of the node. These transformations always apply to the node.

     

    SVG-Viewer needed.

     

    \begin{tikzpicture}
      \tikzstyle{every node}=[draw]
      \draw[style=help lines] (0,0) grid (3,2);
      \draw            (1,0) node{A}
                       (2,0) node[rotate=90,scale=1.5] {B};
      \draw[rotate=30] (1,0) node{A}
                       (2,0) node[rotate=90,scale=1.5] {B};
      \draw[rotate=60] (1,0) node[transform shape] {A}
                       (2,0) node[transform shape,rotate=90,scale=1.5] {B};
    \end{tikzpicture}

11.6 Placing Nodes on a Line or Curve

Until now, we always placed node on a coordinate that is mentioned in the path. Often, however, we wish to place nodes on “the middle” of a line and we do not wish to compute these coordinates “by hand.” To facilitate such placements, TikZ allows you to specify that a certain node should be somewhere “on” a line. There are two ways of specifying this: Either explicitly by using the pos option or implicitly by placing the node “inside” a path operation. These two ways are described in the following.

11.6.1 Explicit Use of the Position Option

There exist styles for specifying positions a bit less “technically”:

11.6.2 Implicit Use of the Position Option

When you wish to place a node on the line (0,0) -- (1,1), it is natural to specify the node not following the (1,1), but “somewhere in the middle.” This is, indeed, possible and you can write (0,0) -- node{a} (1,1) to place a node midway between (0,0) and (1,1).

What happens is the following: 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.

There are two things to note about this: When a node specification is “stored,” its catcodes become fixed. This means that you cannot use overly complicated verbatim text in them. If you really need, say, a verbatim text, you will have to put it in a normal node following the coordinate and add the pos option.

Second, which pos is chosen for the node? The position is inherited from the surrounding scope. However, this holds only for nodes specified in this implicit way. Thus, if you add the option [near end] to a scope, this does not mean that all nodes given in this scope will be put on near the end of lines. Only the nodes for which an implicit pos is added will be placed near the end. Typically, this is what you want. Here are some examples that should make this clearer:

 

SVG-Viewer needed.

 

\begin{tikzpicture}[near end]
  \draw (0cm,4em) -- (3cm,4em) node{A};
  \draw (0cm,3em) --           node{B}          (3cm,3em);
  \draw (0cm,2em) --           node[midway] {C} (3cm,2em);
  \draw (0cm,1em) -- (3cm,1em) node[midway] {D} ;
\end{tikzpicture}

Like the line-to operation, the curve-to operation .. also allows you to specify nodes “inside” the operation. After both the first .. and also after the second .. you can place node specifications. Like for the -- operation, these will be collected and then reinserted after the operation with the pos option set.

11.7 Connecting Nodes

Once you have defined a node and given it a name, you can use this name to reference it. This can be done in two ways, see also Section 8.5. Suppose you have said \path(0,0) node(x) {Hello World!}; in order to define a node named x.

  1. Once the node x has been defined, you can use (x.<anchor>) wherever you would normally use a normal coordinate. This will yield the position at which the given <anchor> is in the picture. Note that transformations do not apply to this coordinate, that is, (x.north) will be the northern anchor of x even if you have said scale=3 or xshift=4cm. This is usually what you would expect.
  2. You can also just use (x) as a coordinate. In most cases, this gives the same coordinate as (x.center). Indeed, if the shape of x is coordinate, then (x) and (x.center) have exactly the same effect.

    However, for most other shapes, some path construction operations like -- try to be “clever” when this they are asked to draw a line from such a coordinate or to such a coordinate. When you say (x)--(1,1), the -- path operation will not draw a line from the center of x, but from the border of x in the direction going towards (1,1). Likewise, (1,1)--(x) will also have the line end on the border in the direction coming from (1,1).

    In addition to --, the curve-to path operation .. and the path operations -| and |- will also handle nodes without anchors correctly. Here is an example, see also Section 8.5:

    SVG-Viewer needed.


    \begin{tikzpicture}
      \path (0,0) node             (x) {Hello World!}
            (3,1) node[circle,draw](y) {$\int_1^2 x \mathrm d x$};

      \draw[->,blue]   (x) -- (y);
      \draw[->,red]    (x) -| node[near start,below] {label} (y);
      \draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {label} (y);
    \end{tikzpicture}

11.8 Predefined Shapes

PGF and TikZ define three shapes, by default:

By loading library packages, you can define more shapes. Currently, the package pgflibraryshapes defines

The exact behaviour of these shapes differs, shapes defined for more special purposes (like a, say, transistor shape) will have even more custom behaviors. However, there are some options that apply to most shapes:

The coordinate shape is handled in a special way by TikZ. When a node x whose shape is coordinate is used as a coordinate (x), this has the same effect as if you had said (x.center). None of the special “line shortening rules” apply in this case. This can be useful since, normally, the line shortening causes paths to be segmented and they cannot be used for filling. Here is an example that demonstrates the difference:

 

SVG-Viewer needed.

 

\begin{tikzpicture}
  \tikzstyle{every node}=[draw]
  \path[yshift=1.5cm,shape=rectangle]
    (0,0) node(a1){} (1,0) node(a2){}
    (1,1) node(a3){} (0,1) node(a4){};
  \filldraw[fill=examplefill] (a1) -- (a2) -- (a3) -- (a4);

  \path[shape=coordinate]
    (0,0) coordinate(b1) (1,0) coordinate(b2)
    (1,1) coordinate(b3) (0,1) coordinate(b4);
  \filldraw[fill=examplefill] (b1) -- (b2) -- (b3) -- (b4);
\end{tikzpicture}