3

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

  • Do we need all of those packages to reproduce the issue? – cfr Apr 18 '17 at 18:14
  • No, of course not. However, I wanted to reproduce the potentially involved environment as close as possible to my actual problem. – raedma Apr 19 '17 at 14:30
2

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}
  • Thanks. I'll try to get deeper into forest to understand what is going on behind the scenes. One thing is still bothering me. For my actual text the parent node is not centered between the children. Manually I can use xhsift in the where level=0{...} defintion. – raedma Apr 19 '17 at 14:29
  • @krtek Are you using, say, calign=midpoint? That's the kind of alignment you want? – cfr Apr 19 '17 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.