2

I can't figure out how to align the following tree properly. The tree itself is not centered within the page and the branches are not aligned vertically nor horizontally.

I'd like the nodes to be aligned on some sort of invisible grid, and the whole thing to be centered in one single page.

Edited for clarification:

  • The pink circle shows that the tree should be centered.
  • The green lines show that these boxes are not aligned horizontally.
  • The blue lines area: these attempt to show the invisible grid along which the nodes should be aligned vertically. In other words, the space between the blue lines should be the same throughout. Right now, for instance, the 8th Level nodes are not aligned with each other. Moreover, the space between the 7th level node and the 8th Level nodes is not the same as the space between the 8th Level nodes and the 9th Level node. Ideally, all of these spaces should be the same throughout. I've tried to adjust the space between the various Levels manually (by adding a centimeter here or 4.5 cm there), but it's not working

enter image description here

\documentclass{book}

\usepackage[linguistics,edges]{forest}


\begin{document}


\begin{forest}
for tree={s sep=8mm,
        inner sep=4,
        l=0,
        forked edge,
        edge={->,>=latex}}
[Priest,rounded rectangle,draw
    [Matador\\ \textit{\small 1st Level},rounded rectangle,draw
        [\textbf{Green Branch},rectangle,draw
            [Junior Junior\\ \textit{\small 2nd Level},rounded rectangle,draw,
                [Chief Eater Booster\\ \textit{\small 8th Level},rounded rectangle,draw,l=130pt
                    [High Chief\\ \textit{\small 10th Level},rounded rectangle,draw,l=2cm
                        [Grand Cookie Horsey\\ \textit{\small 12th Level},rounded rectangle,draw,l=2cm
                            [Obvious Receptacle\\ \textit{\small 15th Level},rounded rectangle,draw,]]]]]]
        [\textbf{Ecclesiastical Branch},rectangle,draw,calign with current
            [Junior Driver\\ \textit{\small 2nd Level},rounded rectangle,draw,
                [Library Leader\\ \textit{\small 8th Level},rounded rectangle,draw,l=4.5cm
                    [Hotel Commandant\\ \textit{\small 10th Level},rounded rectangle,draw,l=2cm
                        [Senior Ant-eater\\ \textit{\small 12th Level},rounded rectangle,draw,l=2cm
                            [Administrative Daddy\\ \textit{\small 15th Level},rounded rectangle,draw,
                                [Mommy\\ \textit{\small 16th-17th Level},rounded rectangle,draw,l=1cm
                                    [Grand Lord\\ \textit{\small 19th-20th Level},rounded rectangle,draw,l=1cm
                                        [Grand Grandiose\\ \textit{\small 20th Level \& up},rounded rectangle,draw]]]]]]]]]
        [\textbf{Military Branch},rectangle,draw
            [Walker\\ \textit{\small 2nd Level},rounded rectangle,draw,
                [Senior Walker\\ \textit{\small 5th Level},rounded rectangle,draw,
                    [Ranking Interviewer\\ \textit{\small 7th Level},rounded rectangle,draw,
                        [Astronaut\\ \textit{\small 9th Level},rounded rectangle,draw,l=2.3cm
                            [Doctor\\ \textit{\small 11th Level},rounded rectangle,draw,l=2cm
                                [Great Doctor\\ \textit{\small 16th Level},rounded rectangle,draw,l=3.5cm]]]]]]]
    ]
]
\end{forest}


\end{document}
  • 1
    It is not fully clear which result you want to get. Could you make a mock-up picture (e.g., in Paint or similar) of how you want it to look? Currently, you specify l=x cm for each node, which puts the nth Level nodes consistently vertically spaced, is this not what you want? – Marijn Aug 15 '17 at 12:49
  • Thanks for the heads up. I've edited the question ... hopefully for clarity! – CyanideBaby Aug 15 '17 at 13:15
  • 1
    With book the page layout itself is asymmetric (the outer margin is larger than the inner), and in addition the tree is wider than the textblock, so it sticks out in the right margin. To center the too wide tree on the textblock, you can use adjustbox or a \makebox, see e.g. tex.stackexchange.com/questions/39435 – Torbjørn T. Aug 15 '17 at 13:17
3

If you say e.g. tier=l2 for several nodes, then Forest will align those nodes (if possible). To centre the tree on the page, you have to do something outside the forest environment, such as using the center environment. However, space is at a premium, so I use \centering here to avoid the extra vertical space. I've also added geometry which defaults to slightly more sensible margins and reduced the separation and inner sep to accommodate everything. I've used fit=band to make sure things don't overlap column-wise and some empty nodes to specify missing tiers. If you don't need to represent missing tiers spatially, you could remove some of these and put the inner sep back to 4. You could also do this if your text height is larger than geometry's default (or if you're using A4, say).

I've added the showframe option to show the text block and margins etc. Obviously, you should remove this in your real document!

\documentclass{book}
\usepackage[showframe]{geometry}
\usepackage[linguistics,edges]{forest}

\begin{document}
\begingroup
\centering
\begin{forest}
  for tree={
    l'=0pt,
    edge={->,>=latex},
    draw,
    fit=band,
    inner sep=2,
  },
  nice empty nodes,
  delay={
    where content={}{edge=-}{},
  },
  where level=2{}{rounded rectangle},
  forked edges,
  [Priest
    [Matador\\\textit{\small 1st Level}, tier=l1
      [\textbf{Green Branch},rounded corners=0pt
        [Junior Junior\\\textit{\small 2nd Level}, tier=l2,
          [Chief Eater Booster\\\textit{\small 8th Level}, tier=l8,
            [High Chief\\\textit{\small 10th Level}, tier=l10,
              [Grand Cookie Horsey\\\textit{\small 12th Level}, tier=l12,
                [Obvious Receptacle\\\textit{\small 15th Level}, tier=l15,]]]]]]
      [\textbf{Ecclesiastical Branch},rectangle,calign with current
        [Junior Driver\\\textit{\small 2nd Level}, tier=l2,
          [Library Leader\\\textit{\small 8th Level}, tier=l8,
            [Hotel Commandant\\\textit{\small 10th Level}, tier=l10,
              [Senior Ant-eater\\\textit{\small 12th Level}, tier=l12,
                [Administrative Daddy\\\textit{\small 15th Level}, tier=l15,
                  [Mommy\\\textit{\small 16th-17th Level}, tier=l17,
                    [, tier=l18 [Grand Lord\\\textit{\small 19th-20th Level}, tier=l19
                      [Grand Grandiose\\\textit{\small 20th Level \& up}, tier=l20]]]]]]]]]]
      [\textbf{Military Branch},rectangle
        [Walker\\\textit{\small 2nd Level}, tier=l2,
          [, tier=l3 [, tier=l4 [Senior Walker\\\textit{\small 5th Level}, tier=l5,
            [, tier=l6 [Ranking Interviewer\\\textit{\small 7th Level}, tier=l7,
              [, tier=l8
                [Astronaut\\\textit{\small 9th Level}, tier=l9,
                  [,tier=l10 [Doctor\\\textit{\small 11th Level}, tier=l11,
                    [,tier=l12 [,tier=l13 [,tier=l14 [,tier=l15 [Great Doctor\\\textit{\small 16th Level}, tier=l16,]]]]]]]]]]]]]]]]
    ]
  ]
\end{forest}
\par
\endgroup
\end{document}

fit tree to page

  • That works very well, thank you. I've edited the numbers assigned to each tier so that they actually don't correspond to the levels mentioned in the nodes. So for instance, the node for level 20 is actually on tier 14. The spaces work nicely and it's all smooth. The only issue I still have is horizontal space between the "Branch" boxes: there's more space between the center box and the right hand box, than between the left box and the center one. Any suggestion for that? – CyanideBaby Aug 15 '17 at 14:00
  • 1
    @nofare They are not equidistant now because one branch has wider children than the other. If you force them to be equidistant, the tree will be more balanced in one way and less balanced in another. However, I think you need, in that case, to do the adjustments later - probably after the tree is packed. Then you can measure the x values and adjust the s values for the nodes. If you want to do this, I can take a look later. – cfr Aug 15 '17 at 16:12

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.