# Growing Trees with Forest package

Is it possible to use the forest package grow a tree so that the children are stacked vertically, like this:

I ended up doing this: If the parent is A and has three children B C D, and I want to attach all the kids as shown above, I had to do this:

\begin{forest}
for tree={
draw,
%  minimum height=2cm,
%  anchor=west,
align=center
% calign
%  child anchor=west
},
[{ROOT},  align=center
[{ A} , parent anchor=south, child anchor=north
[{B}, parent anchor=east,  anchor=east, child anchor=east , calign=child edge
[{C}, parent anchor=east, anchor=east, child anchor=east, calign=child edge
[{D}, parent anchor=east, anchor=east, child anchor=east
]
]
]
]
\end{forest}


As you can see, even though they are all children of A, I had to make B the parent of C and C the parent of D.

• This one is similar: tex.stackexchange.com/questions/337867/… Nov 18, 2016 at 22:38
• This is identical to an existing question ...
– cfr
Nov 18, 2016 at 22:45
• Question seems identical to Diagram using Forest package Nov 19, 2016 at 0:02
• @DaiBowen the diagram is the same as the question you've pointed to. The answer uses the technique that I'm trying to replace, namely, treating siblings as children of eachother rather than all children of the same parent. CFR below gives the solution I was looking for. Nov 21, 2016 at 22:43
• See tex.stackexchange.com/questions/263749/…. But there's Forest v1 code fore this diagram somewhere (with the proper structure).
– cfr
Nov 22, 2016 at 3:01

Not only have several people provided code for this diagram before, I've definitely answered it using Forest before.

It would be much easier if the image in the question was linked to the source since that might well give me a clue.

I know I've given a Forest solution because I found one, but I seem to have not given a link to my answer. And I remember being annoyed because I drew the diagram from scratch when the OP could have linked to code I could have copy-pasted and adapted, since it had been answered before (but maybe the code no longer worked or didn't use Forest or something).

This diagram is intensely annoying as this KEEPS happening to me.

Anyway, I updated the code I've got for Forest v.2 using a modified version of the code in my answer linked in Torbjørn T.'s comment. This version uses the folder style from the edges library.

\documentclass[border=5pt,tikz,multi]{standalone}
\usepackage[edges]{forest}
\forestset{%
colour me out/.style={outer color=#1!75, inner color=#1!50, draw=darkgray, thick, blur shadow, rounded corners},
rect/.append style={rectangle, rounded corners=2pt},
dir tree switch/.style args={at #1}{%
for tree={
edge=-Latex,
font=\sffamily,
fit=rectangle,
},
where level=#1{
for tree={
folder,
grow'=0,
},
delay={child anchor=north},
}{},
before typesetting nodes={
for tree={
content/.wrap value={\strut ##1},
},
if={isodd(n_children("!r"))}{
for nodewalk/.wrap pgfmath arg={{fake=r,n=##1}{calign with current edge}}{int((n_children("!r")+1)/2)},
}{},
},
},
}

\begin{document}
\begin{forest}
dir tree switch=at 1,
for tree={
font=\sffamily\bfseries,
rect,
align=center,
edge+={thick, draw=darkgray},
where level=0{%
colour me out=green!50!white,
}{%
if level=1{%
colour me out=green!95!black,
}{%
colour me out=magenta!50!orange!75!white,
edge+={-Triangle},
},
},
}
[Drawing\\Diagrams
[Defining node\\and arrow styles
[Setting shape]
[Choosing colour]
]
[Positioning\\the nodes
[Using a matrix
[Absolutely]
[Relatively]
[Using overlays]
]
]
[Drawing arrows\\between nodes
[Default arrows
[Arrow library]
[Re-sizing tips]
[Shortening]
[Bending]
]
]
]
\end{forest}
\end{document}


• The accepted answer to the question referenced by DaiBowen treats the siblings as children of each-other to hack the desired effect. My question was specifically how to not do that. I see that while your answer wasn't the accepted one, it is the correct approach. Thank you @cfr for writing it here. Nov 21, 2016 at 22:53
• Having trouble inserting this code into my document. I put the code you gave me in its own file Tree.tex , and \include{Tree.tex} -- but I get errors that the environment forest is undefined. I did add the preamble \usepackage{standalone} in the original document. Am I missing something obvious? Nov 22, 2016 at 2:09
• Yes, but there is a question somewhere with an answer like this. However, this version is updated to use v2 of Forest which is easier. You do not want \include{Tree.tex}. \include is for things like complete chapters. Try \input{Tree}. No need for the extension if it is .tex. Otherwise, do a sanity check: create a new test document and test the code there before inserting it into your main document. That is the first diagnostic step to narrowing down the problem. (I have just spent several hours problem-narrowing myself.)
– cfr
Nov 22, 2016 at 2:48
• I should have mentioned that YES it compiles fine in its own document. The culprit (many hours later) was the standalone preambles function. A question about your code above: I see you set level #1 to grow downwards. How do you set level 2 to grow normally? Using the statement where level=#2 throws an error. Nov 22, 2016 at 18:19
• @Jonathan #1 is the argument passed to the style. dir tree switch=at 1 switches direction at level 1. dir tree switch=at 2 would switch direction at level 2. The default direction of growth is down. grow'=0 sets the direction of growth to the right (i.e. at an angle of 0). You get an error because the style accepts only one argument so #2 doesn't refer to anything. It is hard to know what you are trying to do. If that doesn't answer your question, please ask a follow-up if you want a tree with a different structure. I'm afraid I've never used standalone's premables.
– cfr
Nov 22, 2016 at 22:27