8

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

7

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.

3
  • 1
    So I was right here when I said that it seemed to me forest is not (currently) really designed to do this? This is useful as an example. (I find the documentation too difficult to follow when it lacks examples, I'm afraid, and so end up here...) I find it difficult to understand the 'stages' forest uses and how to use them to effectively shape a tree. (So I pretty much give up if something needs this!) It is good to hear that this may change in future versions. Thanks for all your work - forest is fantastic and I only wish I understood it!
    – cfr
    Jul 18 '14 at 22:19
  • Is there a better way of drawing the tree in the question I linked to? I tried using before computing xy but I ran into trouble with the paths drawn between levels. I got it to look perfect except for a vertical line drawn down the middle of the root node...
    – cfr
    Jul 18 '14 at 22:57
  • 1
    Any news on this?
    – cfr
    Nov 11 '18 at 0:43

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.