The TikZ and PGF Packages
Manual for version 3.1.10
TikZ Library fit ¶
\usetikzlibrary[fit] % ConTeXt
The library defines (currently only two) options for fitting a node so that it contains a set of coordinates.
When you load this library, the following options become available:
/tikz/fit=⟨coordinates or nodes⟩(no default) ¶
/tikz/every fit(style, initially empty) ¶
1. A minimal bounding box containing all coordinates is computed. Note that if a coordinate like (a) is used that contains a node name, this has the same effect as explicitly providing the (a.north) and (a.south) and (a.west) and (a.east). If you wish to refer only to the center of the a node, use (a.center) instead.
2. The text width option is set to the width of this bounding box.
3. The align=center option is set.
4. The anchor is set to center.
5. The at position of the node is set to the center of the computed bounding box.
6. After the node has been typeset, its height and depth are adjusted such that they add up to the height of the computed bounding box and such that the text of the node is vertically centered inside the box.
This option must be given to a node path command. The ⟨coordinates or nodes⟩ should be a sequence of TikZ coordinates or node names, one directly after the other without commas (like with the plot coordinates path operation). Examples are (1,0) (2,2) or (a) (1,0) (b), where a and b are nodes.
For this sequence of coordinates, a minimal bounding box is computed that encompasses all the listed ⟨coordinates or nodes⟩. For coordinates in the list, the bounding box is guaranteed to contain this coordinate, for nodes it is guaranteed to contain the east, west, north and south anchors of the node. In principle (the details will be explained in a moment), things are now set up such that the text box of the node will be exactly this bounding box.
Here is an example: We fit several points in a rectangular node. By setting the inner sep to zero, we see exactly the text box of the node. Then we fit these points again in a circular node. Note how the circle encompasses exactly the same bounding box.
Every time the fit option is used, the following style is also applied to the node:
Set this style to change the appearance of a node that uses the fit option.
The exact effects of the fit option are the following:
The above means that, generally speaking, if the node contains text like box in the above example, it will be centered inside the box. It will be difficult to put the text elsewhere, in particular, changing the anchor of the node will not have the desired effect. Instead, what you should do is to create a node with the fit option that does not contain any text, give it a name, and then use normal nodes to add text at the desired positions. Alternatively, consider using the label or pin options.
Suppose, for instance, that in the above example we want the word “box” to appear inside the box, but at its top. This can be achieved as follows:
Here is a real-life example that uses fitting:
/tikz/rotate fit=⟨angle⟩ (no default, initially 0) ¶
This key fits ⟨coordinates or nodes⟩ inside a node that is rotated by ⟨angle⟩. As a side effect, it also sets the /tikz/rotate key.