制作树木时更改和混合森林包中的文件夹样式

制作树木时更改和混合森林包中的文件夹样式

我有一个关于forest包的问题。我想更改以下树以节省一些垂直空间,方法是将某一级别的文件夹样式更改为交替或以不同的方向增长。对此,我不知道为什么第一级没有垂直对齐。

附加问题:如何实现交替文件夹结构?例如

            A
            |-B
          C-|
or even   E-|-D

我想要的是这样的。

期望输出

这是我的 MWE:

\documentclass[tikz,multi]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows}

\begin{document}
    \begin{forest}
      basic/.style = {draw, thin, drop shadow, font=\sffamily},
      my root/.style = {basic, rounded corners=2pt, fill=black!2},
      upper style/.style = {basic, rounded corners=6pt, fill=black!6, text width=10.5em},
      lower style/.style = {basic, rounded corners=0pt,fill=black!10, text width=9em},
      for tree={%
        parent anchor=south,
        child anchor=north,
        edge path={
          \noexpand\path [-{Stealth[]}, \forestoption{edge}, thin]
            (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
        },
        /tikz/>=LaTeX,
      },
      where level=0{%
        my root,
        %for 1={%
          for tree={%
            if={level()<3}{%
              upper style,
            }{%
              lower style,
            },
            if={level()<2}{%
              if={isodd(n_children())}{%
                calign=child edge,
                calign primary child/.wrap pgfmath arg={#1}{int((n_children()+1)/2)},
              }{%
                calign=edge midpoint,
              },
            }{%
              folder,
              grow'=0,
            },
          },
      }{},
      [Alphabet
        [First five letters, folder, grow'=0,
          [A, lower style,]
          [B, lower style,]
          [C, lower style,]
          [D, lower style,]
          [E, lower style, text width=12em]
        ]
        [Some header]
        [More letters
          [Two
            [F]
            [G]
          ]
          [Nine
            [H]
            [I]
            [J]
            [K]
            [L]
            [M]
            [N]
            [O]
            [And a longer box, text width=12em]
          ]
          [Some more stuff
            [to fill]
            [this tree]
          ]
        ]
      ]
    \end{forest}
\end{document}

谢谢你的时间!

答案1

我认为,这种folder风格在设计时并没有真正考虑到增长方向的变化。因此,事后需要进行一些手动调整。至少,我认为最简单的方法是在绘制树之前移动相关的子树。

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows}
\begin{document}
\begin{forest}
  basic/.style = {draw, thin, drop shadow, font=\sffamily},
  upper style/.style = {basic, rounded corners=6pt, edge+={-Stealth, thin}, fill=black!6, text width=10.5em},
  lower style/.style = {basic, rounded corners=0pt, edge+={-, line width=.4pt}, fill=black!10, text width=9em},
  where level<=2{%
    upper style,
    edge path'={
      (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)
    },
  }{%
    lower style,
  },
  where level<=1{%
    parent anchor=children,
    child anchor=parent,
    if={isodd(n_children())}{%
      calign=child edge,
      calign primary child/.process={
        O+nw+n{n children}{(#1+1)/2}
      },
    }{%
      calign=edge midpoint,
    },
  }{
    folder,
    grow'=0,
  },
  [Alphabet
    [First five letters, folder, grow'=0, for children={lower style},
      before drawing tree={
        tempdima/.option=!r2.max y,
        tempdima-/.option=max y,
        for tree={
          y+/.register=tempdima,
        },
      }
      [A]
      [B]
      [C]
      [D]
      [E, text width=12em]
    ]
    [Some header]
    [More letters
      [Two
        [F]
        [G]
      ]
      [Nine
        [H]
        [I]
        [J]
        [K]
        [L]
        [M]
        [N]
        [O]
        [And a longer box, text width=12em]
      ]
      [Some more stuff
        [to fill]
        [this tree]
      ]
    ]
  ]
\end{forest}
\end{document}

对齐第一级

我认为,如果不改变树的结构,Forest 就无法实现进一步的要求。也就是说,你可以伪造它,但你不能将一些子项放在一个方向,将一些子项放在另一个方向,除非手动移动一个集合或另一个集合,在这种情况下,放弃 Forest 并使用 Ti 可能更容易Z 直接放置所有东西。

如果您准备更改树的结构(您甚至可以自动执行此操作,尽管我不会费心,除非您有非常多的仅具有这种模式的树),您可以使用坐标节点来更改生长方向,即一些子节点将成为其当前父节点的孙节点,并且您将有一个坐标作为其父节点。然后,您需要插入另一组三个坐标,使原始子节点成为其原始父节点的曾孙节点。我怀疑这是否值得增加代码的复杂性,但这里有一个例子。

\begin{forest}
  basic/.style = {draw, thin, drop shadow, font=\sffamily},
  upper style/.style = {basic, rounded corners=6pt, edge+={-Stealth, thin}, fill=black!6, text width=10.5em},
  lower style/.style = {basic, rounded corners=0pt, edge+={-, line width=.4pt}, fill=black!10, text width=9em},
  where level<=2{%
    upper style,
    edge path'={
      (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)
    },
  }{%
    lower style,
  },
  where level<=1{%
    parent anchor=children,
    child anchor=parent,
    if={isodd(n_children())}{%
      calign=child edge,
      calign primary child/.process={
        O+nw+n{n children}{(#1+1)/2}
      },
    }{%
      calign=edge midpoint,
    },
  }{
    folder,
    grow'=0,
  },
  [Alphabet
    [First five letters, folder, grow'=0, for children={lower style},
      before drawing tree={
        tempdima/.option=!r2.max y,
        tempdima-/.option=max y,
        for tree={
          y+/.register=tempdima,
        },
      }
      [A]
      [B]
      [C]
      [D]
      [E, text width=12em]
    ]
    [Some header]
    [More letters
      [Two
        [F]
        [G]
      ]
      [Nine
        [H]
        [I]
        [J]
        [K]
        [, coordinate, grow'=-90, for descendants={edge path'={(!u.parent anchor) |- (.child anchor)}}, before packing={calign primary child=2}
          [, coordinate
            [And a longer box, text width=12em]
          ]
          [, coordinate
            [N]
            [O]
          ]
          [, coordinate
            [L]
            [M]
          ]
        ]
      ]
      [Some more stuff
        [to fill]
        [this tree]
      ]
    ]
  ]
\end{forest}

重构树

顺便问一下,为什么您希望某些边缘具有规则宽度,而另一些边缘具有thin?尽管我在这里保留了它,但我认为这是一个奇怪的想法。

相关内容