3

This is a question from a comment in this question:

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

I'm not getting any answer to my comment, which is why I'm asking a new question now; not sure if this is the right thing to do.

I'm trying to make the edges of the TikZ tree below connect to the nodes (or: boxes) surrounding the leaves. E.g., the edge from Top Node should connect from the bottom of the node tn towards the top of both nodes lhl1s and rhl1s, etc., all the way through the tree. Currently, the edges reach inside the leaves.

My hunch is that I should not be using a tree at all for this - or is there a simple way to do it with \Tree?

\documentclass{article}

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

\begin{document}

\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}[every tree node/.style={align=center,anchor=north},level 2+/.style={level distance=1.25cm,sibling distance=30mm}]
    \Tree [.\node(tn){Top Node};
              [.\node(lhl1s){Left-Hand Level 1 Sister};
              ]
              [.\node(rhl1s){Right-Hand Level 1 Sister};
                  [.\node(rhl2s1){Right-Hand Level 2 Sister};
                  ]
                  [.\node(rhl2s2){Right-Hand Level 2 Sister};
                 ]
                 ]
          ]
    \node[draw,fit=(tn)]{};
    \node[draw,fit=(lhl1s)]{};
    \node[draw,fit=(rhl1s)]{};
    \node[draw,fit=(rhl2s1)]{};
    \node[draw,fit=(rhl2s2)]{};
    \end{tikzpicture}
\caption{A TikZ Tree}
\label{tikz-tree}
\end{center}
\end{figure}

\end{document}
2

Nothing wrong with using a tree I suppose, but instead of adding all the extra nodes to draw the boxes, just draw the border of the default nodes in the tree, by adding draw to the every tree node style.

(I changed from the center environment to \centering, cf. Should I use center or centering for figures and tables?)

As cfr mentions in a comment, because the text in the various nodes (may) have different height and depth, this makes the height of the boxes vary. You could fix that by adding something like text height=2ex,text depth=0.5ex to the every tree node style (for tikz-qtree), or in the for tree={..} (for forest, below). However, you can also be a bit sneaky, and use font=\strut instead.

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{figure}[ht!]
\centering
\begin{tikzpicture}[
  every tree node/.style={
    draw, %%% added
%    inner sep=5pt, % if you want more space inside the nodes
    align=center,
    anchor=north,
    font=\strut},
  level 2+/.style={
    level distance=1.25cm,
    sibling distance=10mm}]
    \Tree [.{Top Node}
              [.{Left-Hand Level 1 Sister}
              ]
              [.{Right-Hand Level 1 Sister}
                  [.{Right-Hand Level 2 Sister}
                  ]
                  [.{Right-Hand Level 2 Sister}
                 ]
                 ]
          ]
    \end{tikzpicture}
\caption{A TikZ Tree}
\label{tikz-tree}
\end{figure}
\end{document}

enter image description here

Using forest you get a more compact tree by default:

\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{figure}[ht!]
\centering
\begin{forest}
for tree={draw,font=\strut}
[Top Node
  [Left-Hand Level 1 Sister]
  [Right-Hand Level 1 Sister
  [Right-Hand Level 2 Sister
  ]
  [Right-Hand Level 2 Sister]
  ]
]
\end{forest}
\caption{A forest Tree}
\label{tikz-tree}
\end{figure}
\end{document}

enter image description here

  • Really nice, thanks for this! I actually like the more "spread-out" look of Tree rather than the more compact look of the forest package. – Sebastian Sulger Jan 9 '17 at 10:52
  • @SebastianSulger You can add fit=rectangle or fit=band to easily get a less compact tree in Forest, which is more flexible generally. – cfr Jan 11 '17 at 1:43
  • (+1) Maybe setting a standard text height would be good here? – cfr Jan 11 '17 at 1:44
  • @cfr Good point, I didn't really notice that. – Torbjørn T. Jan 11 '17 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.