10

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. enter image description here

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}

enter image description here

David Kroik
  • 461
  • 4
  • 13

2 Answers2

9

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}

enter image description here

Ignasi
  • 136,588
6

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.