[Tex/LaTex] Vertical/horizontal alignment with forest

foresthorizontal alignmentvertical alignment

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}

Best Answer

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

Related Question