| 1 |
% Copyright 2019 by Till Tantau |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
|
| 11 |
\section{Fitting Library} |
| 12 |
\label{section-library-fit} |
| 13 |
|
| 14 |
\begin{tikzlibrary}{fit} |
| 15 |
The library defines (currently only two) options for fitting a node so that |
| 16 |
it contains a set of coordinates. |
| 17 |
\end{tikzlibrary} |
| 18 |
|
| 19 |
When you load this library, the following options become available: |
| 20 |
|
| 21 |
\begin{key}{/tikz/fit=\meta{coordinates or nodes}} |
| 22 |
This option must be given to a |node| path command. The \meta{coordinates |
| 23 |
or nodes} should be a sequence of \tikzname\ coordinates or node names, |
| 24 |
one directly after the other without commas (like with the |
| 25 |
|plot coordinates| path operation). Examples are |(1,0) (2,2)| or |
| 26 |
|(a) (1,0) (b)|, where |a| and |b| are nodes. |
| 27 |
|
| 28 |
For this sequence of coordinates, a minimal bounding box is computed that |
| 29 |
encompasses all the listed \meta{coordinates or nodes}. For coordinates in |
| 30 |
the list, the bounding box is guaranteed to contain this coordinate, for |
| 31 |
nodes it is guaranteed to contain the |east|, |west|, |north| and |south| |
| 32 |
anchors of the node. In principle (the details will be explained in a |
| 33 |
moment), things are now set up such that the text box of the node will be |
| 34 |
exactly this bounding box. |
| 35 |
|
| 36 |
Here is an example: We fit several points in a rectangular node. By setting |
| 37 |
the |inner sep| to zero, we see exactly the text box of the node. Then we |
| 38 |
fit these points again in a circular node. Note how the circle encompasses |
| 39 |
exactly the same bounding box. |
| 40 |
% |
| 41 |
\begin{codeexample}[preamble={\usetikzlibrary{fit}}] |
| 42 |
\begin{tikzpicture}[inner sep=0pt,thick, |
| 43 |
dot/.style={fill=blue,circle,minimum size=3pt}] |
| 44 |
\draw[help lines] (0,0) grid (3,2); |
| 45 |
\node[dot] (a) at (1,1) {}; |
| 46 |
\node[dot] (b) at (2,2) {}; |
| 47 |
\node[dot] (c) at (1,2) {}; |
| 48 |
\node[dot] (d) at (1.25,0.25) {}; |
| 49 |
\node[dot] (e) at (1.75,1.5) {}; |
| 50 |
|
| 51 |
\node[draw=red, fit=(a) (b) (c) (d) (e)] {box}; |
| 52 |
\node[draw,circle,fit=(a) (b) (c) (d) (e)] {}; |
| 53 |
\end{tikzpicture} |
| 54 |
\end{codeexample} |
| 55 |
|
| 56 |
Every time the |fit| option is used, the following style is also applied to |
| 57 |
the node: |
| 58 |
% |
| 59 |
\begin{stylekey}{/tikz/every fit (initially \normalfont empty)} |
| 60 |
Set this style to change the appearance of a node that uses the |fit| |
| 61 |
option. |
| 62 |
\end{stylekey} |
| 63 |
|
| 64 |
The exact effects of the |fit| option are the following: |
| 65 |
% |
| 66 |
\begin{enumerate} |
| 67 |
\item A minimal bounding box containing all coordinates is computed. |
| 68 |
Note that if a coordinate like |(a)| is used that contains a node |
| 69 |
name, this has the same effect as explicitly providing the |
| 70 |
|(a.north)| and |(a.south)| and |(a.west)| and |(a.east)|. If you |
| 71 |
wish to refer only to the center of the |a| node, use |(a.center)| |
| 72 |
instead. |
| 73 |
\item The |text width| option is set to the width of this bounding box. |
| 74 |
\item The |align=center| option is set. |
| 75 |
\item The |anchor| is set to |center|. |
| 76 |
\item The |at| position of the node is set to the center of the |
| 77 |
computed bounding box. |
| 78 |
\item After the node has been typeset, its height and depth are |
| 79 |
adjusted such that they add up to the height of the computed |
| 80 |
bounding box and such that the text of the node is vertically |
| 81 |
centered inside the box. |
| 82 |
\end{enumerate} |
| 83 |
% |
| 84 |
The above means that, generally speaking, if the node contains text like |
| 85 |
|box| in the above example, it will be centered inside the box. It will be |
| 86 |
difficult to put the text elsewhere, in particular, changing the |anchor| |
| 87 |
of the node will not have the desired effect. Instead, what you should do |
| 88 |
is to create a node with the |fit| option that does not contain any text, |
| 89 |
give it a name, and then use normal nodes to add text at the desired |
| 90 |
positions. Alternatively, consider using the |label| or |pin| options. |
| 91 |
|
| 92 |
Suppose, for instance, that in the above example we want the word ``box'' |
| 93 |
to appear inside the box, but at its top. This can be achieved as follows: |
| 94 |
% |
| 95 |
\begin{codeexample}[preamble={\usetikzlibrary{fit}}] |
| 96 |
\begin{tikzpicture}[inner sep=0pt,thick, |
| 97 |
dot/.style={fill=blue,circle,minimum size=3pt}] |
| 98 |
\draw[help lines] (0,0) grid (3,2); |
| 99 |
\node[dot] (a) at (1,1) {}; |
| 100 |
\node[dot] (b) at (2,2) {}; |
| 101 |
\node[dot] (c) at (1,2) {}; |
| 102 |
\node[dot] (d) at (1.25,0.25) {}; |
| 103 |
\node[dot] (e) at (1.75,1.5) {}; |
| 104 |
|
| 105 |
\node[draw=red,fit=(a) (b) (c) (d) (e)] (fit) {}; |
| 106 |
\node[below] at (fit.north) {box}; |
| 107 |
\end{tikzpicture} |
| 108 |
\end{codeexample} |
| 109 |
|
| 110 |
Here is a real-life example that uses fitting: |
| 111 |
% |
| 112 |
\begin{codeexample}[preamble={\usetikzlibrary{fit,shapes.geometric}}] |
| 113 |
\begin{tikzpicture} |
| 114 |
[vertex/.style={minimum size=2pt,fill,draw,circle}, |
| 115 |
open/.style={fill=none}, |
| 116 |
sibling distance=1.5cm,level distance=.75cm, |
| 117 |
every fit/.style={ellipse,draw,inner sep=-2pt}, |
| 118 |
leaf/.style={label={[name=#1]below:$#1$}},auto] |
| 119 |
|
| 120 |
\node [vertex] (root) {} |
| 121 |
child { node [vertex,open] {} |
| 122 |
child { node [vertex,open] {} |
| 123 |
child { node [vertex] (b's parent) {} |
| 124 |
child { node [vertex] {} |
| 125 |
child { node [vertex,leaf=d] {} } |
| 126 |
child { node [vertex,leaf=e] {} } } |
| 127 |
child { node [vertex,leaf=b] {} } } |
| 128 |
child { node [vertex,leaf=a] {} } } |
| 129 |
child { node [coordinate] {} |
| 130 |
child[missing] |
| 131 |
child { node [vertex] (f's parent) {} |
| 132 |
child { node [vertex,leaf=c] {} } |
| 133 |
child { node [vertex,leaf=f] {} } } } |
| 134 |
edge from parent node {$\rho$} }; |
| 135 |
|
| 136 |
\node [fit=(d) (e) (b) (b's parent),label=above left:$F^{(b,R)}$] {}; |
| 137 |
\node [fit=(c) (f) (f's parent),label=above right:$F^{(c,R)}$] {}; |
| 138 |
\end{tikzpicture} |
| 139 |
\end{codeexample} |
| 140 |
% |
| 141 |
\end{key} |
| 142 |
|
| 143 |
\begin{key}{/tikz/rotate fit=\meta{angle} (initially 0)} |
| 144 |
This key fits \meta{coordinates or nodes} inside a node that is rotated by |
| 145 |
\meta{angle}. As a side effect, it also sets the |/tikz/rotate| key. |
| 146 |
% |
| 147 |
\begin{codeexample}[preamble={\usetikzlibrary{fit}}] |
| 148 |
\begin{tikzpicture}[inner sep=0pt,thick, |
| 149 |
dot/.style={fill=blue,circle,minimum size=3pt}] |
| 150 |
\draw[help lines] (0,0) grid (3,2); |
| 151 |
\node[dot] (a) at (1,1) {}; |
| 152 |
\node[dot] (b) at (2,2) {}; |
| 153 |
\node[dot] (c) at (1,2) {}; |
| 154 |
\node[dot] (d) at (1.25,0.25) {}; |
| 155 |
\node[dot] (e) at (1.75,1.5) {}; |
| 156 |
\node[draw, fit=(a) (b) (c) (d) (e)] {}; |
| 157 |
\node[draw=red, rotate fit=30, fit=(a) (b) (c) (d) (e)] {}; |
| 158 |
\end{tikzpicture} |
| 159 |
\end{codeexample} |
| 160 |
% |
| 161 |
\end{key} |