Debian Codegraph @

texmf-dist/doc/generic/pgf/pgfmanual-en-library-trees.tex

raw
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{Tree Library}
12 \label{section-tree-library}
13
14 \begin{tikzlibrary}{trees}
15 This packages defines styles to be used when drawing trees.
16 \end{tikzlibrary}
17
18
19 \subsection{Growth Functions}
20
21 The package |trees| defines two new growth functions. They are installed using
22 the following options:
23
24 \begin{key}{/tikz/grow via three points=\texttt{one child at (}\meta{x}
25 \texttt{) and two children at (}\meta{y}\texttt{) and (}\meta{z}|)|%
26 }
27 This option installs a growth function that works as follows: If a parent
28 node has just one child, this child is placed at \meta{x}. If the parent
29 node has two children, these are placed at \meta{y} and \meta{z}. If the
30 parent node has more than two children, the children are placed at points
31 that are linearly extrapolated from the three points \meta{x}, \meta{y},
32 and \meta{z}. In detail, the position is $x + \frac{n-1}{2}(y-x) +
33 (c-1)(z-y)$, where $n$ is the number of children and $c$ is the number of
34 the current child (starting with~$1$).
35
36 The net effect of all this is that if you have a certain ``linear
37 arrangement'' in mind and use this option to specify the placement of a
38 single child and of two children, then any number of children will be
39 placed correctly.
40
41 Here are some arrangements based on this growth function. We start with a
42 simple ``above'' arrangement:
43 %
44 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
45 \begin{tikzpicture}[grow via three points={%
46 one child at (0,1) and two children at (-.5,1) and (.5,1)}]
47 \node at (0,0) {one} child;
48 \node at (0,-1.5) {two} child child;
49 \node at (0,-3) {three} child child child;
50 \node at (0,-4.5) {four} child child child child;
51 \end{tikzpicture}
52 \end{codeexample}
53
54 The next arrangement places children above, but ``grows only to the
55 right''.
56 %
57 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
58 \begin{tikzpicture}[grow via three points={%
59 one child at (0,1) and two children at (0,1) and (1,1)}]
60 \node at (0,0) {one} child;
61 \node at (0,-1.5) {two} child child;
62 \node at (0,-3) {three} child child child;
63 \node at (0,-4.5) {four} child child child child;
64 \end{tikzpicture}
65 \end{codeexample}
66
67 In the final arrangement, the children are placed along a line going down
68 and right.
69 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
70 \begin{tikzpicture}[grow via three points={%
71 one child at (-1,-.5) and two children at (-1,-.5) and (0,-.75)}]
72 \node at (0,0) {one} child;
73 \node at (0,-1.5) {two} child child;
74 \node at (0,-3) {three} child child child;
75 \node at (0,-4.5) {four} child child child child;
76 \end{tikzpicture}
77 \end{codeexample}
78
79 These examples should make it clear how you can create new styles to
80 arrange your children along a line.
81 \end{key}
82
83 \begin{key}{/tikz/grow cyclic}
84 This style causes the children to be arranged ``on a circle''. For this,
85 the children are placed at distance |\tikzleveldistance| from the parent
86 node, but not on a straight line, but on points on a circle. Instead of a
87 sibling distance, there is a |sibling angle| that denotes the angle between
88 two given children.
89 %
90 \begin{key}{/tikz/sibling angle=\meta{angle}}
91 Sets the angle between siblings in the |grow cyclic| style.
92 \end{key}
93 %
94 Note that this function will rotate the coordinate system of the children
95 to ensure that the grandchildren will grow in the right direction.
96 %
97 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
98 \begin{tikzpicture}
99 [grow cyclic,
100 level 1/.style={level distance=8mm,sibling angle=60},
101 level 2/.style={level distance=4mm,sibling angle=45},
102 level 3/.style={level distance=2mm,sibling angle=30}]
103 \coordinate [rotate=-90] % going down
104 child foreach \x in {1,2,3}
105 {child foreach \x in {1,2,3}
106 {child foreach \x in {1,2,3}}};
107 \end{tikzpicture}
108 \end{codeexample}
109 %
110 \end{key}
111
112 \begin{key}{/tikz/clockwise from=\meta{angle}}
113 This option also causes children to be arranged on a circle. However, the
114 rule for placing children is simpler than with the |grow cyclic| style: The
115 first child is placed at \meta{angle} at a distance of
116 |\tikzleveldistance|. The second child is placed at the same distance from
117 the parent, but at angle \meta{angle}${}-{}$|\tikzsiblingangle|. The third
118 child is displaced by another |\tikzsiblingangle| in a clockwise fashion,
119 and so on.
120
121 Note that this function will not rotate the coordinate system.
122 %
123 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
124 \begin{tikzpicture}
125 \node {root}
126 [clockwise from=30,sibling angle=30]
127 child {node {$30$}}
128 child {node {$0$}}
129 child {node {$-30$}}
130 child {node {$-60$}};
131 \end{tikzpicture}
132 \end{codeexample}
133 %
134 \end{key}
135
136 \begin{key}{/tikz/counterclockwise from=\meta{angle}}
137 Works the same way as |clockwise from|, but sibling angles are added
138 instead of subtracted.
139 \end{key}
140
141
142 \subsection{Edges From Parent}
143
144 The following styles can be used to modify how the edges from parents are
145 drawn:
146
147 \begin{stylekey}{/tikz/edge from parent fork down}
148 This style will draw a line from the parent downwards (for half the level
149 distance) and then on to the child using only horizontal and vertical
150 lines.
151 %
152 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
153 \begin{tikzpicture}
154 \node {root}
155 [edge from parent fork down]
156 child {node {left}}
157 child {node {right}
158 child[child anchor=north east] {node {child}}
159 child {node {child}}
160 };
161 \end{tikzpicture}
162 \end{codeexample}
163 %
164 \end{stylekey}
165
166 \begin{stylekey}{/tikz/edge from parent fork right}
167 This style behaves similarly, only it will first draw its edge to the
168 right.
169 %
170 \begin{codeexample}[preamble={\usetikzlibrary{trees}}]
171 \begin{tikzpicture}
172 \node {root}
173 [edge from parent fork right,grow=right]
174 child {node {left}}
175 child {node {right}
176 child {node {child}}
177 child {node {child}}
178 };
179 \end{tikzpicture}
180 \end{codeexample}
181 %
182 \end{stylekey}
183
184 \begin{stylekey}{/tikz/edge from parent fork left}
185 Behaves similarly to the previous styles.
186 \end{stylekey}
187
188 \begin{stylekey}{/tikz/edge from parent fork up}
189 Behaves similarly to the previous styles.
190 \end{stylekey}
191
192
193 %%% Local Variables:
194 %%% mode: latex
195 %%% TeX-master: "pgfmanual-pdftex-version"
196 %%% End: