# Drawing a frame or box around parts of a syntactic tree using tikz-qtree

I'm trying to make boxes around parts of my tree, but I find no command to do so. I have tried the qtree package, which has the !qframesubtree that can do the job, but only for complete subtrees. What I want to do is to draw one box around the upper part of the tree (including the IP) and another box around the rest of the tree (including the VP).

\begin{exe}
\ex \Tree [.IP Spec [.I\1 I [.VP Spec [.V\1 V XP ] ] ] ]
\end{exe}


Any tips on how to do this.

I include a simple drawing of the desired result.

EDIT: With the comments below I have now solved the problem. This is the code I ended up with:

\documentclass{minimal}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=bottom{}}},
background tree/.style={for tree={text opacity=0.2,draw opacity=0.2,}},
}
\begin{document}
\begin{forest} sn edges,
[IP, tikz={\node [draw, fit=()(Spec)(Ibar)(I)]{};}, s sep=3em,
[{Spec}, name=Spec]
[I',name=Ibar, l sep=2em, s sep=6.5em,
[I, name=I ]
[VP, tikz={\node [draw, inner sep=0.03em, fit=()(!1)(!ll)]{};}
[Spec]
[V',
[V]
[XP]] ]] ]
\end{forest}
\end{document}


If you can accept forest instead of qtree, it's easy to do something similar to what you want.

tikz={\node [draw, red, fit=()(!1)(!ll)]{};} is an option which calls TiKZ to draw a node which fits the present node, its first child and last grandchild.

The green square could be also written with tikz={\node [draw, green, fit to tree]{};}

\documentclass[border=3mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={s sep=5mm, fit=band}
[IP, tikz={\node [draw, red, fit=()(!1)(!ll)]{};},
[Spec]
[I [I]
[VP, tikz={\node [draw, green, fit=()(!1)(!ll)]{};}
[Spec]
[V' [V]
[XP]]]]]
\end{forest}
\end{document}


Whilst I myself would recommend using the forest solution above, here's a way to do it in tikz-qtree by using the fit library that comes together with tikz to fit tikz's nodes into a box.

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{fit}

\begin{document}

\begin{tikzpicture}
\Tree [.\node(ip){IP}; \node(specip){Spec}; [.\node(ibar){I$'$}; \node(i){I}; [.\node(vp){VP}; \node(specvp){Spec}; [.V$'$ V \node(xp){XP}; ] ] ] ]
\node[draw,fit=(vp)(specvp)(xp)]{};
\node[draw,fit=(ip)(specip)(ibar)(i)]{};
\end{tikzpicture}

\end{document}


EDIT: I've edited the code to address your question in the comment to this answer. Note that it is also possible to use tikzset` to tweak the distance between the tree's nodes so as to stop the boxes touching the nodes they aren't meant to be touching.

• Many thanks! A furhter question. Is there any way to make the VP escape the upper box? – David Kroik Mar 19 '15 at 9:19
• @DavidKroik Indeed it is. See the edited answer. – Pavel Rudnev Mar 19 '15 at 9:32
• @PavelRudnev, I am using your solution - works nicely so far. One question though: Is there an easy way to attach the tree edges to the top of the boxes? Currently, the edges reach inside the boxes. Thanks! – Sebastian Sulger Jan 3 '17 at 14:01