2

This is probably a stupid question for anyone well-versed in TikZ, but I'm not well-versed in TikZ and just want to create som simple tree structures. Hoping for a quick fix.

My trees typically have no labels on internal nodes, just leaves. I tried qtree like this:

{\newcommand{\qleafhook}{\scriptsize}
\Tree [.
[ [ 0 1 ] [ 2 3  ] ]
[ [ 4 [ 5 6 ] ] [ [ 7 8 ] [ 9 10 ] ] ]
]
}

Pretty nice, almost what I want, except that I'd like nodes on the same depth be aligned horizontally, i.e., I'd like just two leaf levels here: one for 0 to 4 (which have depth 3), and another for 5 to 10 (which have depth 4).

qtree version

It didn't seem like qtree was designed for fixing this, so I switched to trying forest. I quickly arrived at this:

{\scriptsize
\begin{forest}
  [, for tree={parent anchor=north, child anchor=north}
  [ [ [0] [1] ] [ [2] [3]  ] ]
  [ [ [4] [ [5] [6] ] ] [ [ [7] [8] ] [ [9] [10] ] ] ]
  ]
\end{forest}
}

This is exactly what I want, except that it's huge, lots of space for the edge labels that don't exist, and also more horizontal space than I would like.

enter image description here

Is there an easy way to squash this down? When I fiddle around with l and l sep, it breaks the vertical alignment I want, and also doesn't have that much effect. I understand that I could find how to do it if I took a deep dive in the Forest and/or TikZ manuals, but I'm hoping there's something relatively simple that I'm missing, that somebody could point out.

3

The perhaps simplest way to reduce the level distance is to add l+=<something negative> but this does not allow you to make the distance arbitrarily small. To improve on that you can pass these options to the children.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={parent anchor=north, child anchor=north,font=\scriptsize,fit=tight,
for children={l sep-=1.5em,l-=1.5em}}
  [
  [ [ [0] [1] ] [ [2] [3]  ] ]
  [ [ [4] [ [5] [6] ] ] [ [ [7] [8] ] [ [9] [10] ] ] ]
  ]
\end{forest}
\end{document}

enter image description here

If you want something closer to your first picture, fix the edge angles.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={parent anchor=north, child anchor=north,font=\scriptsize,fit=tight,
calign=fixed edge angles,calign angle=70,
for children={l sep-=2em,l-=2em}}
  [
  [ [ [0] [1] ] [ [2] [3]  ] ]
  [ [ [4] [ [5] [6] ] ] [ [ [7] [8] ] [ [9] [10] ] ] ]
  ]
\end{forest}
\end{document}

enter image description here

  • Also, it breaks the alignment. In your figure, 5 and 6 are now below 7 to 10. – Jesper Larsson May 6 at 15:49
  • @JesperLarsson yes, good catch, fixed it. – user121799 May 6 at 15:56
  • Thanks, some stuff to play around with in your example there. I'll make do with that for now in combination with resizebox, in the absence of The Perfect Solution. – Jesper Larsson May 6 at 18:09

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.