# TikZ forest - Drawing upon a subtree

I would like to add a rectangle upon a subtree like in the following picture. How to do that ?

Here is the forest code.

\documentclass{article}
\usepackage{forest}
\useforestlibrary{linguistics}

\begin{document}
\pagestyle{empty}

\begin{forest}
for tree = {
sn edges,
grow'=0,
l=2.5cm,
s sep=0.2cm,
anchor=west,
child anchor=west}
[
[O
[R
[T]
]
[T
[R]
]
]
[R
[O
[T]
]
[T
[O]
]
]
[T
[O
[R]
]
[R
[O]
]
]
]
\end{forest}

\end{document}


You can assign names to nodes in forest, so you can use this together with the Tikz fit library to draw a node around the nodes you have chosen.

If you don't want to use a node, you can also just do (doesn't require any library):

\draw[red,thick] (bottomright.south east) rectangle (left.west|-topright.north);


## Code

\documentclass[margin=10pt]{standalone}
\usepackage{forest}
\useforestlibrary{linguistics}
\usetikzlibrary{fit}

\begin{document}
\begin{forest}
for tree = {
sn edges,
grow'=0,
l=2.5cm,
s sep=0.2cm,
anchor=west,
child anchor=west}
[
[O,name=left
[R
[T,name=topright]
]
[T
[R,name=bottomright]
]
]
[R
[O
[T]
]
[T
[O]
]
]
[T
[O
[R]
]
[R
[O]
]
]
]
\node[draw=red,thick,fit=(left)(topright)(bottomright)] {};
\end{forest}
\end{document}

• That's great and easy to use ! Thanks for this. Sep 13, 2016 at 22:13
• @projetmbc I added an alternative, see edit. :) Sep 13, 2016 at 22:14
• If the aim is to enclose all children of a node, you can also apply tikz={\node [draw,red,fit to=tree] {};} directly to that node. Sep 13, 2016 at 22:25
• @projetmbc Or , tikz+={\node [draw=red, fit=() (!u) (!uu)] {};} to circle the node, the node's parent and the node's grandparent, for example.
– cfr
Sep 13, 2016 at 23:10
• @LukasCB Use labels, like label={above:<test>} in the node options. Sep 15, 2016 at 19:19