森林:调整树边缘路径

森林:调整树边缘路径

我想画一棵如图所示的树此主题。但是,我的节点中的文本可能很长,导致换行。然后,anchor=150线程中的选项会弄乱grandchildren节点的水平对齐。当我使用时,anchor=west水平对齐很好,但节点向右移动并越过下一列。

我该怎么做才能避免这种情况?


平均能量损失

\documentclass{standalone}

\usepackage{forest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
% \usetikzlibrary{fpu}
\usetikzlibrary{intersections}
\usetikzlibrary{trees}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usetikzlibrary{shadows}
\useforestlibrary{edges}

\tikzset{
  parent/.style={align=center,text width=4cm,fill=gray!50,rounded corners=2pt},
  child/.style={align=center,text width=2.5cm,fill=gray!20,rounded corners=6pt},
  grandchild/.style={fill=white,text width=2.3cm}
}

\begin{document}

\begin{forest}
  for tree={%
    thick,
    drop shadow,
    l sep=0.8cm,
    s sep=1.0cm,
    node options={
      draw,
      font=\sffamily
    },
    edge={
      semithick,
      -Latex
    },
    where level=0{parent}{},
    where level=1{
      minimum height=1cm,
      child,
      parent anchor=south west,
      tier=p,
      l sep=0.45cm,
      for descendants={%
        grandchild,
        minimum height=0.6cm,
        anchor=west,
        edge path={
          \noexpand\path[\forestoption{edge}]
          (!to tier=p.parent anchor) |-(.child anchor)\forestoption{edge label};
        },
      }
    }{},
  }
  [\large Long text with line break%
    [\textbf{Test 1} \\ with a lot of subtext%
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
    [\textbf{Test 2} \\ with a lot of subtext% 
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
    [\textbf{Test 3} \\ with a lot of subtext%
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
    [\textbf{Test 4} \\ with a lot of subtext%
      [Topic%
      [Long topic with line break%
      [Topic%
      ]]]%
    ]
  ]     
\end{forest}

\end{document}

结果

在此处输入图片描述

答案1

您正在加载edges库但实际上并未使用它,这很奇怪,因为这正是它支持的样式。此外,它消除了伪造树结构的需要,使您可以在标记中保留直观的父子关系。

在这种情况下,folder样式是显而易见的选择,至少忽略其误导性的名称。唯一的问题是,当它只应用于特定级别之外时,它不会做正确的事情,而且由于它试图以一种聪明的方式做正确的事情,所以你必须在树的处理后期覆盖它。

例如,

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shadows,arrows.meta}
\tikzset{
  parent/.style={align=center,text width=4cm,fill=gray!50,rounded corners=2pt},
  child/.style={align=center,text width=2.5cm,fill=gray!20,rounded corners=6pt},
  grandchild/.style={fill=white,text width=2.3cm}
}
\begin{document}
\begin{forest}
  for tree={%
    thick,
    drop shadow,
    node options={
      draw,
      font=\sffamily
    },
    edge={
      semithick,
      -Latex
    },
    where level=0{
      parent,
      l sep'=0.8cm,
      s sep'=1.0cm,
    }{
      folder,
      grow'=0,
    },
    where level=1{
      minimum height=1cm,
      child,
      l sep=7.5mm,
      for descendants={%
        grandchild,
        minimum height=0.6cm,
      },
      for children={
        before computing xy={s+=5mm},
      }
    }{},
  }
  [\large Long text with line break%
    [\textbf{Test 1} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
    [\textbf{Test 2} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
    [\textbf{Test 3} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
    [\textbf{Test 4} \\ with a lot of subtext%
      [Topic]
      [Long topic with line break]
      [Topic]
    ]
  ]
\end{forest}
\end{document}

相关内容