每一层都有不同分支的树,其中某些叶子上有 tikz 和方程式

每一层都有不同分支的树,其中某些叶子上有 tikz 和方程式

我想画一棵这样的树:

不同层级上有不同枝干类型的树

第一层水平放置,第二层垂直放置,然后在第三层及更高层再次水平堆叠。到目前为止,我一直在尝试这个答案:如何在 TikZ 树或图表中向下和向右分叉

但我无法像我希望的那样修改第 3 级。

这是我的代码(来自如何在 TikZ 树或图表中向下和向右分叉):

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[edges]{forest}
\forestset{%
  dir tree switch forking/.style args={at #1}{%
    for tree={
      font=\sffamily,
      fit=rectangle,
    },
    where level=#1{
      for tree={
        folder,
        grow'=0,
      },
      delay={
        child anchor=north,
        !u.parent anchor=south,
        edge path'={(!u.parent anchor) -- ++(0,-\forestoption{fork sep}) -| (.child anchor)}
      },
    }{
      if={level()<(#1)}{
        forked edge,
        parent anchor=children,
        child anchor=parent,
      }{},
    },
    before typesetting nodes={
      for tree={
        content/.wrap value={\strut ##1},
      },
      if={isodd(n_children("!r"))}{
        for nodewalk/.wrap pgfmath arg={{fake=r,n=##1}{calign with current edge}}{int((n_children("!r")+1)/2)},
      }{},
    },
  },
  node title/.style={
    content={\textbf{#1}},
  },
  node descriptor/.style={content+={\\#1}},
  title splitter/.style={
    before typesetting nodes={
      split option={content}{:}{node title,node descriptor},
    },
  },
  title split/.style={
    for tree={title splitter},
  },
}
\begin{document}
\begin{forest}
  dir tree switch forking=at 1,
  title split,
  where level=0{align=center},
  for tree={draw},
  [Title
    [Level 1
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2
            [Level 3]
            [Level 3]
            [Level 3]
            [Level 3]
        ]
        [Level 2]
    ]
    [Level 1
        [Level 2]
        [Level 2
            [Level 3]
            [Level 3]
            [Level 3]
            [Level 3]
            [Level 3]
            [Level 3]
            [Level 3
                [Level 4]
                [Level 4]
            ]
            ]
        ]
    [Level 1
      [Level 2]
    ]
  ]
\end{forest}
\end{document}

但是,这存在一个问题:当我在其中一个插槽中写入方程式时,PDF 无法编译。是否可以在“森林”分支中写入方程式?

谢谢 !

答案1

你错过了{text width=30mm}。你可以更改此参数的内容,但不能删除该参数,因为where level=<number>{<arg>}{<arg>}必须采用该形式。包含数学没有一般问题。你可以将其用作math content节点(或全局)的选项,也可以使用$...$\(...\)。显示数学将是另一个问题。

包含几个内联方程的工作示例

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[edges]{forest}
\usepackage{amsmath}
\forestset{%
  dir tree switch forking/.style args={at #1}{%
    for tree={
      font=\sffamily,
      fit=rectangle,
    },
    where level=#1{
      for tree={
        folder,
        grow'=0,
      },
      delay={
        child anchor=north,
        !u.parent anchor=south,
        edge path'={(!u.parent anchor) -- ++(0,-\forestoption{fork sep}) -| (.child anchor)}
      },
    }{
      if={level()<(#1)}{
        forked edge,
        parent anchor=children,
        child anchor=parent,
      }{},
    },
    before typesetting nodes={
      for tree={
        content/.wrap value={\strut ##1},
      },
      if={isodd(n_children("!r"))}{
        for nodewalk/.wrap pgfmath arg={{fake=r,n=##1}{calign with current edge}}{int((n_children("!r")+1)/2)},
      }{},
    },
  },
  node title/.style={
    content={\textbf{#1}},
  },
  node descriptor/.style={content+={\\#1}},
  title splitter/.style={
    before typesetting nodes={
      split option={content}{:}{node title,node descriptor},
    },
  },
  title split/.style={
    for tree={title splitter},
  },
}
\begin{document}
\begin{forest}
  dir tree switch forking=at 1,
  title split,
  where level=0{align=center}{align=left},
  for tree={draw},
  [Title
    [Level 1
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2]
        [Level 2:describe]
        [Level 2
          [{$x^2 + 1 = 0$}]
            [Level 3]
            [Level 3]
            [Level 3]
        ]
        [Level 2]
    ]
    [Level 1
      [Level 2]
      [Level 2
      [{$\Pr (A/B) = \frac{\Pr(B/A)}{\Pr(B)}$} ]
          [Level 3]
          [Level 3]
          [Level 3]
          [Level 3]
          [Level 3]
          [Level 3
              [Level 4]
              [Level 4]
          ]
        ]
    ]
    [Level 1
      [Level 2]
    ]
  ]
\end{forest}
\end{document}

相关内容