与森林的垂直/水平对齐

与森林的垂直/水平对齐

我不知道如何正确对齐下面的树。树本身不在页面中央,树枝也没有垂直或水平对齐。

我希望节点能够在某种不可见的网格上对齐,并且整个节点能够在一个页面中居中。

编辑以澄清:

  • 粉色圆圈表示树应该位于中心。
  • 绿线显示这些框没有水平对齐。
  • 蓝线区域:这些区域试图显示节点应垂直对齐的不可见网格。换句话说,蓝线之间的空间应该始终相同。例如,现在,第 8 级节点彼此不对齐。此外,第 7 级节点和第 8 级节点之间的空间与第 8 级节点和第 9 级节点之间的空间不同。理想情况下,所有这些空间应始终相同。我曾尝试手动调整各个级别之间的空间(在这里增加 1 厘米或在那里增加 4.5 厘米),但不起作用

在此处输入图片描述

\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

如果您说例如tier=l2多个节点,那么 Forest 将会对齐这些节点(如果可能)。要将树置于页面中央,您必须在环境之外执行某些操作forest,例如使用center环境。但是,空间非常宝贵,因此我\centering在这里使用 以避免额外的垂直空间。我还添加了geometry默认值,使其更合理一些,并减少了分离和inner sep以容纳所有内容。我已经习惯fit=band确保事物不会在列方面重叠,并使用一些空节点来指定缺失的层。如果您不需要在空间上表示缺失的层,则可以删除其中一些并将其放回inner sep4如果您的文本高度大于geometry的默认值(或者如果您使用的是 A4),您也可以这样做。

我添加了showframe显示文本块和边距等的选项。显然,您应该在实际文档中删除它!

\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}

使树适合页面

相关内容