Forest
Reference
3.17 Anchors¶
Forest defines several anchors which can be used with any TikZ node belonging to a Forest tree (manually added TikZ nodes are thus excluded).
parent anchor(anchor)¶
child anchor(anchor)¶
anchor(anchor)¶
These anchors point to coordinates determined by node options parent anchor, child anchor and anchor.
parent,(anchor)¶
parent’, ,
parent first,(anchor)¶
parent first’, ,
first,(anchor)¶
first’
children first,(anchor)¶
children first’, ,
children,(anchor)¶
children’, ,
children last,(anchor)¶
children last’, ,
last,(anchor)¶
last’
parent last,(anchor)¶
anchor parent faces the parent node (or, in case of the root, where the parent would be);
anchor children faces the children (or, in case of a node without children, where the children would be);
anchor first faces the first child (or … you get it, right?);
anchor last faces the last child (or … you know!).
when first and last are combined with parent into parent_first and parent_last, they refer to the first and last child of the parent node, i.e. siblings of the current node.
parent, ,
Growth direction based anchors.
TikZ’s “compass anchors” east, north etc. resolve to coordinates on the border of the node facing east, north etc. (for the shapes that define these anchors). The above Forest’s anchors are similar in that they also resolve to coordinates on the border of the node. However, the “cardinal directions” are determined by the growth direction of the tree in the node and its parent:
Combinations like children_first work like combinations of compass directions, e.g. north_west, but note that
While first and last always point in opposite directions, parent and children do not do so if the growth direction of the tree changes in the node, i.e. if the node’s grow differs from it’s parent’s grow. Thus in general, it is useful to have anchors -parent and -children, which point in the opposite directions as parent and children, respectively, and their combinations with first and last.
The ' variants refer precisely to the point where the cardinal growth direction intersects the border. Variants without ' snap to the closest compass anchor of the node.
These anchors work as expected even if the node is rotated, or if the children are reversed.
For simple examples, see definitions of sn edges and roof; for more involved examples, see the edges library.
\def\redorblue#1{\expandafter\redorbluei#1\END}%
\def\redorbluei#1#2\END{\expandafter\ifx#1-red\else blue\fi}%
\forestset{
draw anchors/.style n args=3{% #1=above, #2=below, #3='-variant of anchor?
tikz={
\foreach \a in {first,last,parent first,parent last,children,children last,#1}
{\fill[](.\a#3)circle[radius=1pt] node[above,font=\tiny,color=\redorblue\a]{\a#3};}
\foreach \a in {-parent first,-parent,-parent last,-children,-children first,#2}
{\fill[](.\a#3)circle[radius=1pt] node[below,font=\tiny,color=\redorblue\a]{\a#3};}
}
},
draw anchors/.default={parent,children first}{-children last}{},
}
\begin{forest}
for tree={
minimum width=10em, minimum height=13ex, s sep+=5em,
draw, draw anchors,
font=\tt, delay={content/.process=OOw2{grow}{reversed}{grow\ifnum#2=1'\fi=#1}}
}
[
[,rotate=20,draw anchors={parent,children first}{-children last}{'}]
[,for tree={grow'=0}, l sep+=5em, draw anchors={-children last}{parent,children first}{}
[,rotate=-60]
[]
]
]
\end{forest}