2

I would like to change the position of a specific child node in a diagram using the forest package.

I would like to have a diagram organized as follows, with a child node located directly to the left/right of the root node:

enter image description here

So far I'm getting the following. I can't seem to move the child node directly to the left/right of the root node:

enter image description here

How to get the desired result?

Here is a MWE :

\documentclass[border=2mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
    \begin{forest}
    for tree = {
        text width=30mm,
        if level=0{fill=gray!70}{fill=gray!20},
        if level=1{fill=gray!50}{},
        forked edge,
    }
    [Blah Blah Blah
        [Should be left of the root node, grow'=west
            [Blah Blah Blah
            ]
        ]
        [Blah Blah Blah
            [Blah Blah Blah
                [Blah Blah Blah 
                    [Blah Blah Blah
        ]]]]
        [Blah Blah Blah
            [Blah Blah Blah 
                [Blah Blah Blah
        ]]]
        [Blah Blah Blah
            [Blah Blah Blah
                [Blah Blah Blah
                    [Blah Blah Blah
         ]]]]
    ]
\end{forest}
\end{document}
2
  • 1
    It will be better that you would ask new question with link to answer on your original question, which completely fulfill your problem. New problem, new question, please!
    – Zarko
    Commented Mar 26, 2022 at 12:18
  • 1
    I posted a more general solution with much simpler syntax and no manual placement of nodes. Your example can be found there.
    – Sandy G
    Commented May 6, 2022 at 16:15

2 Answers 2

2

Update

A more general solution that requires no manual placement of nodes is now available here.

Old Solution:

Forest isn't designed for this construction, so you have to do some manual placement:

enter image description here

For the two special nodes (left of root and right of root), manually adjust the coordinates using before computing xy={l=0,s=±3.75cm}. Change the s if you change other spacing factors in your tree.

Then use no edge to prevent the edge from being drawn in a way you really don't want, but name the nodes you want to connect. At the end, add extra edges using a \draw command.

I also adjusted the s sep and l sep at the beginning to make the two drawn horizontal edges the same length as the vertical automatic edges. Finally, I added fork sep so the forking occurs at half the l sep length.

\documentclass[border=2mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
    \begin{forest}
    for tree = {
        text width=30mm,
        if level=0{fill=gray!70}{fill=gray!20},
        if level=1{fill=gray!50}{},
        forked edge,
        s sep=5mm, l sep=5mm,
        fork sep=2.5mm
    }
    [Root,name=n1
        [West of root,before computing xy={l=0,s=-3.75cm},no edge,grow=west,name=n2
            [Far west
            ]
        ]
        [Blah Blah Blah
            [Blah Blah Blah
        ]]
        [Blah Blah Blah
            [Blah Blah Blah 
                [Blah Blah Blah
        ]]]
        [Blah Blah Blah
            [Blah Blah Blah
        ]]
        [East of root,before computing xy={l=0,s=3.75cm},no edge,grow=east,name=n3
            [Far east
         ]]
    ]
    \draw(n1)--(n2) (n1)--(n3);
\end{forest}
\end{document}
5

I suggest the following approach. Let the Should be left of the root node node be the parent of "the root", and the Blah Blah Blah node on its left be its parent in turn (so the actual root of the tree). The desired tree geometry can then be achieved by setting grow=east on these special "children"; and it's easy to set their style to what we want anyway.

Under the new hierarchy, it is easiest to set the styles of the "normal children" of the old root by relative level(>). Also note that forked edge should be now set only for the real descendants of the old root.

\documentclass[border=2mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
  level0/.style={fill=gray!70},
  level1/.style={fill=gray!50},
  level2/.style={fill=gray!20},
  for tree={
    text width=30mm, anchor=center,
  },
  [Blah Blah Blah, grow=east, level2,
    [Should be left of the root node, grow=east, level1,
      [Blah Blah Blah,
        level0,
        for relative level={1}{level1},
        for relative level>={2}{level2},
        for descendants={forked edge},
        [Blah Blah Blah
          [Blah Blah Blah
            [Blah Blah Blah 
              [Blah Blah Blah
              ]]]]
        [Blah Blah Blah
          [Blah Blah Blah 
            [Blah Blah Blah
            ]]]
        [Blah Blah Blah
          [Blah Blah Blah
            [Blah Blah Blah
              [Blah Blah Blah
              ]]]]
      ]]]
\end{forest}
\end{document}
1
  • Hello, thank you for your answer, however I was hoping for a more general solution. I have edited my question accordingly.
    – Bastien
    Commented Mar 26, 2022 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .