I've been making tikz pictures for a while now, and there seem to be a number of different ways to position nodes within a diagram. Absolute coordinates, east/west, left/right, the $ $ things, minimum distance... Although I can find enough information on the internet to somehow get stuff to work, I'm having trouble seeing the big picture. Can anybody explain how all these different methods for node positioning work together, and how the computer actually figures out where the nodes should go?
1 Answer
You need to differentiate a few things:
The
at (<somewhere)
part (or as a key:at={(<somewhere>)}
).The
<somewhere>
can be any coordinate specification that is allowed by TikZ.This may be an explicit coordinate like
(1, 2)
,(30: 4)
or<another node>.<with an anchor>
.
This may also be an implicit coordinate like
(xyz cs: x=1, y=2)
,(xyz polar cs: angle=30, radius=4)
or(node cs: name=<another node>, anchor=<with an anchor>)
.
The concept of coordinate system and their version (explicit vs implicit) are explained in section 13.2 “Coordinate Systems” on pp. 123ff. of the PGF manual.
The
at
part can also be any arbitrary calculations withcalc
’s$…$
notion which is explained in section 13.5 “Coordinate Calculations” on pp. 134ff. of the PGF manual.Furthermore, you can give options at the begin of the coordinate specification which transform the specified coordinate, oftentimes, these are options like
scale
,shift
,xscale
and so on. These are the same options that can also be given on a path or a scope.
But this has nothing specifically to do with nodes because every coordinate on a path can be given in one of the mentioned ways.
If the node has no specified
at
part, the current position on the path is used (which is the origin at the start of the path).So, the two following paths do the same:
\path ( 3, 2) coordinate (a) + ( 1, 0) coordinate (b) + ( 0, 1) coordinate (c) + (-1, 0) coordinate (d) + ( 0,-1) coordinate (e); \coordinate (a) at (3, 2) coordinate (b) at + (right:1) coordinate (c) at + ( up:1) coordinate (d) at + ( left:1) coordinate (e) at + ( down:1);
The
anchor=<anchor>
option (default:center
).This specifies a point of the node itself that shall be placed at the
at
.Options like
above left
andright
(from here on called the<somewhere>
keys) internally set the anchor (above left
setsanchor=south east
,right
setsanchor=west
). If a length is given with this option (above left=1cm
) a transformation in that direction is added. With our example, this means that the.south east
anchor of the node is placed one centimeter away from theat
part in the directionabove left
(135°).The
<somewhere> of=<another node without an anchor>
options (which are deprecated).This key simply combine the
<somewhere>
keys with theat
part. You could also write:at=(<another node without an anchor>),% the `.center` anchor is used anyway <somewhere>=<node distance>
where
<node distance>
is the node distance set by thenode distance
key.They also set the anchor to
.center
but you can change that, of course, if you just use theanchor
key after the<somewhere> of
keys.The
<somewhere>
keys that are enhanced/improved with thepositioning
library (should really be a default library if you work with a “noded” diagram).If the value includes the text
of
, some magic will happen (otherwise it’s just an ordinary<somewhere>=<dimension>
key-value pair, I think).I believe the magic is already explained in another answer of mine. In summary: the
at
part is set (possibly specified with an anchor if an anchor-less node name is given), theanchor
is set (which is alwayscenter
if theon grid
option is set), a transformation is also applied (which is dependent of the content of thenode distance
key (with or withoutand
) and the actual direction (“<somewhere>
”)).The
pos
key, yet another magical thing.If the
pos
key is used on a node, the node is positioned along a (part of a) path. This is already explained in section 16.8 “Placing Nodes on a Line or Curve Explicitly” in pp. 190ff. of the PGF manual.In the same section, the related keys
auto
andswap
as well assloped
andallow upside down
are explained and short-cuts for thepos
key are listed.Section 16.9 “Placing Nodes on a Line or Curve Implicitly” on pp. 193f. explain how that all plays in role if you use the node before the target of a path operator.
at (<somewhere>)
part (or as a key) which hasn’t much to do with nodes. 1b. Absolute and relative (+
/++
) coordinates which have nothing to do with nodes. 2. The anchors (with theanchor
option or internally withabove left
orright
). 3a. Positioning with the deprecated… of
options (which internally specifies anat
and a transformation). 3b. Positioning with thepositioning
library and its…=… of …
options and thenode distance
key (which internally specifies anat
, an anchor and a transformation).