숲: 나무 가장자리 경로 조정

숲: 나무 가장자리 경로 조정

나는 그림과 같은 나무를 그리고 싶다.이 스레드. 그러나 내 노드의 텍스트가 길어서 줄 바꿈이 발생할 수 있습니다. 그런 다음 anchor=150스레드의 옵션이 노드의 수평 정렬을 엉망으로 만듭니다 grandchildren. 수평 정렬을 사용하면 anchor=west문제가 없지만 노드가 오른쪽으로 이동하여 다음 열을 교차합니다.

이를 방지하려면 어떻게 해야 합니까?


MWE

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

관련 정보