2

I'm trying to recreate the following diagram from a book:

original diagram - a Tree

I tried using tikz's graphdrawing package:

\documentclass{standalone}
\usepackage{tikz} 

\usetikzlibrary {graphs, quotes, graphdrawing} \usegdlibrary{trees}
%\usetikzlibrary {arrows.meta,graphs,graphdrawing} \usegdlibrary {layered}

\begin{document} 
\begin{tikzpicture}
 \graph [
 tree layout, 
% layered layout,
 grow down, branch right, 
 nodes={circle ,draw, minimum size=.65cm, inner sep=0pt, very thick},
 level/.style={
  sibling distance = .15cm,
  sibling sep = 0.25cm,
  level distance = 0cm,
  level sep = 0.1cm  
  }, 
 level 3/.style={
  sibling distance = .0cm,
  sibling sep = 0.6cm,
  level distance = 0cm,
  level sep = 0.1cm  
  }  
 ] {
1--2--{3,4--{6,7},5--{8--{10,11},9}}
 };
\end{tikzpicture}
 
\end{document}

Trying first the tree layout engine:

enter image description here

and then I tried the layered layout engine:

enter image description here

They're pretty close, but neither nails it. The original's layout seems to be a mixture of the two. Notice how the subgraph with 5 as root is layed out as a binary tree, while the 1-2-4 chain is what you get with the layered layout.

I also noticed that in the original, the 4 node sits a tad lower than its siblings. While the use of a different sibling seperation in level 3 in the layered layout places 4 a bit off-center wrt 2 for some reason.

The mammoth tikz manual didn't tell me how to:

  1. control level pre distance per node
  2. switch between layouts (probably not possible), or
  3. alternatively, nudging the leftmost child manually so the rest of the tree aligns properly.

Advice appreciated (including achieving this more easily with something else short of illustrator other than tikz)

2 Answers 2

4

If you can consider using forest, this task is rather straightforward. You can just change the calign in the tree. In this example, we can set

for tree={calign child=2}

(or calign=last in this example) in the subtree starting from 4.

\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={circle, minimum size=.65cm, inner sep=0pt,draw,thick} 
 [1
  [2
   [3]
   [4,for tree={calign child=2}
    [5]
    [6]
   ]
   [5
    [8
     [10]
     [11]
    ]
    [9]
   ]
  ]
 ]
\end{forest}
\end{document}

enter image description here

Another version with vertically aligned 2 and 4 nodes.

\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={circle, minimum size=.65cm, inner sep=0pt,draw,thick} 
 [1
  [2
   [3]
   [4,for tree={calign child=2},for current={calign with current edge}
    [5]
    [6]
   ]
   [5
    [8
     [10]
     [11]
    ]
    [9]
   ]
  ]
 ]
\end{forest}
\end{document}

enter image description here

6
  • The 4-6 node isn't aligned with the 1-2 chain like the original. I'm accepting because using the forest package led to a solution. It took some tinkering but 'forest' provides far more control than stock tikz. I put the final code in a separate answer. Thanks.
    – mackmist
    Commented Apr 2, 2021 at 0:57
  • 1
    @mackmist Thanks for notifying me! Yes, your solution is pretty! Meanwhile I added another variation which also addresses the problem.
    – user238301
    Commented Apr 2, 2021 at 1:09
  • Better, but notice that now 2 is not centered between 3 and 5.
    – mackmist
    Commented Apr 2, 2021 at 1:40
  • Just for the records: I am not going to modify the answer. The OP decided to unacceptable it, likely because they added some requirements which they wanted to get addressed, too. Anyone else spending their time on other people's problems should be aware of that.
    – user238301
    Commented Apr 3, 2021 at 6:48
  • Suggesting I use the forest package was helpful and I upvoted (and initially accepted) your answer for that reason. But, the question is about faithfully recreating the reference layout and your answer doesn't do that.
    – mackmist
    Commented Apr 3, 2021 at 9:15
4

Thanks to @user238301's suggestion to use the forest package, I was able to recreate the exact layout.

Note the details of the original layout:

  • 2 but must be centered between 3 and 5
  • 4-7 must be aligned directly below 1-2
  • 6 and 7 must be "pushed" left so 7 is below 4, while 8 and 9 must be placed symmetrically about 5 (and below)

Getting this right was harder than expected.

Here's the reproduction and the original side-by-side (which is which? :-) ):

enter image description here enter image description here

And here's the code:

\documentclass{article} 
\usepackage{forest} 
\begin{document} 
\begin{forest}
for tree={circle, minimum size=.65cm, inner sep=0pt,draw, very thick,edge={line width=1.5pt}}
 [1,for tree={l sep=0.15cm, l=0cm}
  [2,calign=center, for tree={l sep=0.05cm, s sep=0.3cm}
   [3]
   [4, before computing xy={s=0mm},for tree={calign=last, s sep=0.05cm}
    [6]
    [7]
   ]
   [5,for tree={calign=center, s sep=0.05cm}
    [8
     [10]
     [11]
    ]
    [9]
   ]
  ]
 ]
\end{forest}
\end{document}
\end{document}
1
  • Just for the records, I do not think that this is a reasonable answer to the question. You move things around by shifting them by hard-coded distances. It think that you should have specified that you are seeking for a solution of this type in the question.
    – user238301
    Commented Apr 3, 2021 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .