# Boxing labels across branches in LaTeX trees

I have inserted a tree diagram in a LaTeX document which I have drawn using the qtree package:

...
...
\begin{document}
...
\Tree [.{A} [.{B} {C} {D} ] [.{B'} {D'} ] ]
...
\end{document}


This is what the output looks like:

So far, so good.

What I would need to do now is wrap the final nodes of the tree into (labeled) boxes in a way that cuts across the syntactic branching of the tree.

To give you an idea, this is a very rough sketch of what I would like to achieve (where a and b stand for the box labels):

I understand that this might be unattainable with qtree since the !qframesubtree only allows you to put boxes around complete subtrees.

Any suggestions as to how to get around the problem?

• This is really best done with one of the TikZ based tree drawing packages. qtree is really old now. I would recommend forest which can do this trivially. – Alan Munn Jul 24 '17 at 15:50
• I am not sure how this coud be achieved using forest either, since forest seems to also require specifying the child nodes of the current subtree to be boxed, not those of other subtrees. – poxx Jul 24 '17 at 16:03

Here's a forest solution.

\documentclass[]{article}
\usepackage[linguistics]{forest}
\forestset{child frame/.style={tikz={\node () [rectangle, red,draw, fit=#1,inner sep = 0pt] {};}}}
\begin{document}
\begin{forest}
[A [B [C,name=C,child frame={(C),label=below:a} ] [D,name=D ] ] [B',child frame={(D)(D1),label=right:b} [D',name=D1 ] ]]
\end{forest}

\end{document}


• @poxx If you remove the red from my style description, you can add your own colour or dashed etc. to the child frame={ ... } style on any individual node. As with all TikZ things, if you're using things repeatedly, it makes sense to make a style for it. E.g. \tikzset{rd/.style={red,dashed,thick} and then add rd to the child frame options. Because these are passed to tikz notforest they are set using tikzset not forestset. – Alan Munn Jul 24 '17 at 17:14
• (+1) I'd recommend tikz+ so that child frame could be used more than once or in combination with some other wanted effect. But I guess this isn't terribly likely. – cfr Jul 26 '17 at 22:31
• @cfr So e.g. if D itself had a tikz= option, this would simply add to it? I've never needed this in practice, but I see the point. So much to learn about forest. – Alan Munn Jul 26 '17 at 22:36
• In that case, it would depend on the order. If the tikz is processed first, the tikz+ would add to it. If the tikz+ is processed first, the tikz would zap it. tikz sets it. tikz+ adds to whatever is there, if anything. I have sometimes needed this and, in earlier versions, you could only manage 2 lots (with tikz and afterthought). I haven't needed afterthought in a long time! – cfr Jul 26 '17 at 22:41
• It is especially useful in the case of the root node, as you might want to set multiple tikz fragments from there. – cfr Jul 26 '17 at 22:42

Here's a modified version of Alan Munn's approach, using fit to. The first tree uses named nodes. The second doesn't bother.

% addaswyd o ateb Alan Munn: https://tex.stackexchange.com/a/383043/
\documentclass[border=10pt]{standalone}
\usepackage[linguistics]{forest}
\forestset{
child frame/.style={
tikz+={
\node [draw=blue, fit to={#1}, inner sep=0pt] {};
},
},
}
\begin{document}
\begin{forest}
[A [B [C, name=C, child frame={name=C}, label=below:a ] [D, name=D ] ] [B', child frame={{name=D, name=D1}}, [D', name=D1, label=right:b ] ]]
\end{forest}
\begin{forest}
[A [B [C, child frame={c}, label=below:a ] [D ] ] [B' [D', child frame={c,fake={us},l}, label=right:b ] ]]
\end{forest}
\end{document}


Oh, and mine are blue ...