[Tex/LaTex] Forest: Adjust tree edge path

foresttrees

I want to draw a tree like the one shown in this thread. However, the text in my nodes may be long and lead to line breaks. Then, the anchor=150 option from the thread messes up the horizontal alignment of the grandchildren nodes. When I use anchor=west the horizontal alignement is fine, but the nodes shifted to the right and cross the next column.

What can I do to avoid that?


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}

Result

enter image description here

Best Answer

You are loading the edges library but not actually using it, which is odd since this is precisely the kind of style it supports. Moreover, it does away with the need to fake the structure of the tree, allowing you to keep the intuitive parent-child relationships in your mark-up.

In this case, the folder style is the obvious choice, at least ignoring its misleading name. The only issue really is that it doesn't do the right thing when it applies only beyond a certain level and, because it tries to do the right thing in a clever way, you have to override it late in the processing of the tree.

For example,

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