编辑

编辑

我想将第一个节点 Sing(f)=V(x,y,z) 水平居中。到目前为止,我尝试过的所有方法都不起作用,但坦率地说,我对 Forest 了解不多。

我的代码:

\documentclass[a4paper,landscape]{scrartcl}
\usepackage[margin=1in]{geometry}
\usepackage{forest}

\begin{document}
\begin{forest}
  my edge label/.style={
if={greater(n(),n_children("!u")/2)}{
  if={equal(n(),int((n_children("!u")+1)/2))}{
    edge label={node [pos=.6] {#1}},
  }{
    edge label={node [pos=.4, right] {#1}},
  },
}{
  edge label={node [pos=.4, left] {#1}}
},
  },
  for tree={
inner sep=1pt,
text centered,
align=center,
font=\sffamily\bfseries\small,
edge={font=\tiny},
if n children=3{calign=child, calign child=2}{},
s sep+=5pt,
l sep+=15pt,
  }
  [{$Sing(f)=V(x,y,z)$}
[{$Sing(f_x)=V(x,y_1)$}
  [{$Sing(f_{xx})=V(x,y_2,z_1)$}
    [{$Sing(f_{xxx})=V(x,y_3,z_2)$}
      [{$Sing(f_{xxxx})=V(x,y_4,z_3)$}
    [{$Sing(f_{xxxxx})=V(x,y_5,z_4)$}]
    [{$Sing(f_{xxxxy_4})=\emptyset$}]
    [{$Sing(f_{xxxxz_3})=V(x_1,y_5,z_3)$}]]
      [{$Sing(f_{xxxy_3})=\emptyset$}]
      [{$Sing(f_{xxxz_2})=V(x_1,y_4,z_2)$}]]
    [{$Sing(f_{xxy_2})=\emptyset$}]
    [{$Sing(f_{xxz_1})=\emptyset$}]]
  [{$Sing(f_{xy_1})=\emptyset$}]]
[{$Sing(f_y)=\emptyset$},fit=band]
[{$Sing(f_z)=V(y_1,z)$}
  [{$Sing(f_{zy_1})=\emptyset$}]
  [{$Sing(f_{zz})=\emptyset$}]]]
\end{forest}
\end{document}

我得到:

在此处输入图片描述

我将非常感激任何帮助和提示!

答案1

请注意,您为 s 定义的样式edge label从未使用过,因此我在本答案的示例中省略了它。对于edge=\tiny和 也类似text centered

我不太清楚您说的居中是什么意思 - 相对于什么居中?如果页面居中,那么整个树就会非常不平衡。

但是,如果您想让树居中并使其适合页面,您可以随时调整其宽度。但是,您必须非常小心,因为如果材料完全超出页面,TeX 不会警告您。

以下是我认为你可能想要的输出类型:

页面上的树

代码:

\documentclass[a4paper,landscape]{scrartcl}
\usepackage[margin=1in]{geometry}
\usepackage{forest}

\begin{document}
{\centering
\makebox[0pt]{%
\begin{forest}
  for tree={
    inner sep=1pt,
    align=center,
    font=\sffamily\bfseries\small,
    if n children=3{calign=child, calign child=2}{},
    s sep+=5pt,
    l sep+=15pt,
  }
  [{$Sing(f)=V(x,y,z)$}
[{$Sing(f_x)=V(x,y_1)$}
  [{$Sing(f_{xx})=V(x,y_2,z_1)$}
    [{$Sing(f_{xxx})=V(x,y_3,z_2)$}
      [{$Sing(f_{xxxx})=V(x,y_4,z_3)$}
    [{$Sing(f_{xxxxx})=V(x,y_5,z_4)$}]
    [{$Sing(f_{xxxxy_4})=\emptyset$}]
    [{$Sing(f_{xxxxz_3})=V(x_1,y_5,z_3)$}]]
      [{$Sing(f_{xxxy_3})=\emptyset$}]
      [{$Sing(f_{xxxz_2})=V(x_1,y_4,z_2)$}]]
    [{$Sing(f_{xxy_2})=\emptyset$}]
    [{$Sing(f_{xxz_1})=\emptyset$}]]
  [{$Sing(f_{xy_1})=\emptyset$}]]
[{$Sing(f_y)=\emptyset$},fit=band]
[{$Sing(f_z)=V(y_1,z)$}
  [{$Sing(f_{zy_1})=\emptyset$}]
  [{$Sing(f_{zz})=\emptyset$}]]]
\end{forest}}\par}
\end{document}

请注意,这与以下情况无关forest:树对于页面来说太宽了。任何太大的东西都会带来类似的问题 ;)。

编辑

另一种让树适合页面的方法是重新考虑其结构。有几种可能性。一种是改变节点的内部结构,使它们分成两行:

改变节点的内部结构

代码:

\documentclass[a4paper,landscape]{scrartcl}
\areaset{240mm}{152mm}% allow roughly the same total area for margins as you'd get with 1in margins, but let KOMA do the dividing and laying out of the page
\usepackage{array,forest}

\begin{document}
{\centering
\begin{forest}
  for tree={
    inner sep=1pt,
    align={@{}>{$}c<{$}@{}},
    font=\sffamily\bfseries\small,
    if n children=3{calign=child, calign child=2}{},
    s sep+=5pt,
    l sep+=15pt,
  }
  [{Sing(f)\\=V(x,y,z)}
    [{Sing(f_x)\\=V(x,y_1)}
      [{Sing(f_{xx})\\=V(x,y_2,z_1)}
        [{Sing(f_{xxx})\\=V(x,y_3,z_2)}
          [{Sing(f_{xxxx})\\=V(x,y_4,z_3)}
            [{Sing(f_{xxxxx})\\=V(x,y_5,z_4)}]
            [{Sing(f_{xxxxy_4})\\=\emptyset}]
            [{Sing(f_{xxxxz_3})\\=V(x_1,y_5,z_3)}]
          ]
          [{Sing(f_{xxxy_3})\\=\emptyset}]
          [{Sing(f_{xxxz_2})\\=V(x_1,y_4,z_2)}]
        ]
        [{Sing(f_{xxy_2})\\=\emptyset}]
        [{Sing(f_{xxz_1})\\=\emptyset}]
      ]
      [{Sing(f_{xy_1})\\=\emptyset}]
    ]
    [{Sing(f_y)\\=\emptyset},fit=band]
    [{Sing(f_z)\\=V(y_1,z)}
      [{Sing(f_{zy_1})\\=\emptyset}]
      [{Sing(f_{zz})\\=\emptyset}]
    ]
  ]
\end{forest}\par}
\end{document}

或者,您可以保留内部节点结构并重新构建节点之间的关系。例如:

重组树内节点之间的关系

代码:

\documentclass[a4paper,landscape]{scrartcl}
\areaset{240mm}{152mm}% allow roughly the same total area for margins as you'd get with 1in margins, but let KOMA do the dividing and laying out of the page
\usepackage{forest}
\begin{document}
{\centering
\begin{forest}
  for tree={
    align=center,
    math content,
    font=\sffamily\bfseries\small,
    if n children=3{calign=child, calign child=2}{},
    s sep+=5pt,
    l sep+=5pt,
    if={level>2}{
      if n=1{
        l+=20pt
      }{
        if n'=1{
          l+=20pt
        }{
          if n children=0{
            append={[,phantom]}
          }{}
        }
      },
    }{
    },
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
    },
    parent anchor=south,
    child anchor=north,
  }
  [{Sing(f)=V(x,y,z)}
    [{Sing(f_x)=V(x,y_1)}
      [{Sing(f_{xx})=V(x,y_2,z_1)}
        [{Sing(f_{xxx})=V(x,y_3,z_2)}
          [{Sing(f_{xxxx})=V(x,y_4,z_3)}
            [{Sing(f_{xxxxx})=V(x,y_5,z_4)}]
            [{Sing(f_{xxxxy_4})=\emptyset}]
            [{Sing(f_{xxxxz_3})=V(x_1,y_5,z_3)}]
          ]
          [{Sing(f_{xxxy_3})=\emptyset}]
          [{Sing(f_{xxxz_2})=V(x_1,y_4,z_2)}]
        ]
        [{Sing(f_{xxy_2})=\emptyset}]
        [{Sing(f_{xxz_1})=\emptyset}]
      ]
      [{Sing(f_{xy_1})=\emptyset}]
    ]
    [{Sing(f_y)=\emptyset}]%,fit=band]
    [{Sing(f_z)=V(y_1,z)}
      [{Sing(f_{zy_1})=\emptyset}]
      [{Sing(f_{zz})=\emptyset}]
    ]
  ]
\end{forest}\par}
\end{document}

这是否能算作将根节点相对于任何事物置于中心……这是另一个问题。

相关内容