[Tex/LaTex] Forest: different growing directions for branches on the same level

foresttikz-trees

Problem

In TikZ, one can control the growing direction of a branch (and its sub-branches) by using the grow=<direction> key. In forest, however, the grow key only controls the growing direction of the sub-branches but not their parent branch.

How do I make branches on the same level in a forest tree grow in different directions? In the following MWE, for instance, how can I get child 2 to extend horizontally from root in it does in the TikZ tree?

MWE

\documentclass{article}
\usepackage{tikz,forest}

\begin{document}
\texttt{grow} used in a Ti\textit{k}Z tree

\begin{tikzpicture}
  \node{root}
    child{node{child 1}}
    child[grow=east]{node{child 2}
      child child child
    }
  ;
\end{tikzpicture}

\vskip20pt
\texttt{grow} used in a Forest tree

\begin{forest}
  [root
    [child 1]
    [child 2,grow=east
      [][][]
    ]
  ]
\end{forest}
\end{document}

enter image description here

Best Answer

The desired effect can be achieved by moving the subtree of node child 2 by hand. I can see two ways of doing this.

  1. Change the relative position (l and s) of child 2 just before stage compute xy. Note that child 2's l and s are coordinates in the root's ls-coordinate system (see the documentation for options l and s). Since these coordinates are relative to the parent, it is only necessary to change them for the subtree's root, child 2.

  2. Change the absolute position (x and y) of all nodes in child 2's subtree just before stage draw tree.

    A note. In the example, y is adjusted so that child 2 is vertically aligned to root: since this is achieved by calculating the difference between root's and child 2's y, node child 2 must be moved after its descendants.

Note that in both approaches, forest first positions the root's children in the default, -90 degree growth direction, which can in principle influence both the position (note that child 1 is left of the root) and internal structure of the subtrees.

\documentclass{article}
\usepackage{forest}

\begin{document}

% the ls way
\begin{forest}
  [root
    [child 1
      [][][]
    ]
    [child 2, for tree={grow=0},
      before computing xy={l=0,s=2cm}
      [][][]
    ]
  ]
\end{forest}

% the xy way
\begin{forest}
  [root
    [child 1
      [][][]
    ]
    [child 2, for tree={grow=0},
      before drawing tree={for descendants=
        {x+=1cm, y+=y("!r")-y("!r2")},
        x+=1cm, y+=y("!r")-y("!r2")
      }
      [][][]
    ]
  ]
\end{forest}

\end{document}

To be honest, I don't find any of the two approaches elegant. My first thought was to embed two forest environments, one for each subtree, into a tikzpicture environment. Since forest works by spitting out tikz code, I reasoned it should be possible to position the root nodes manually using tikz's mechanisims (at,right of, etc.). (Note that begin draw and end draw should be emptied for this to have any chance of success.) However, the result was not as expected ... I'll investigate the reasons and try to fix the issue in some future version of forest.

Related Question