6

Is it possible to change this MWE which is using forest package to produce a tree, similar to that produced by the next MWE that is not using forest? The very last level of child nodes would have to be stacked on top of each other with edge drawn from the south of the parent to the west of the child.

After reading the Forest manual, I got an impression that the key where n children=0 should be able to help, and added the line with the comment, but no matter what grow parameter I supplied, such as north/south, or 0-45-90-120 etc, the direction of growth did not change.

The solution probably lies in the pp. 10-12 of the Forest manual, but it is evading me :(

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest} 
for tree={
  where n children=0{draw=red,grow=120}{}, %<============
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- +(0,-15pt) -|   
      (.child anchor)\forestoption{edge label};
  },
  l sep=10pt,
} 
[P
  [N1
    [N1C1]
    [N1C2]
  ]
  [N2
    [N2C1]
    [N2C2]
  ]
]
\end{forest}
\end{document}

Without forest (the lowest level edge is different and the nodes are stacked):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}[
    children/.style={grow=down,xshift=-0.2cm,
        edge from parent path={(\tikzparentnode.201) |- (\tikzchildnode.west)}
    },
    level1/.style ={level distance=4em,anchor=west},
    level2/.style ={level distance=8em,anchor=west},
    level 1/.style={edge from parent fork down,sibling distance=5em,level distance=4em
    }
]
    \node[anchor=south](super){P}[]
    child{node {N1}
        child[children,level1] {node {N1C1}}
        child[children,level2] {node {N1C2}}
    }
    child{node {N2}
        child[children,level1] {node {N2C1}}     
        child[children,level2] {node {N2C2}}};
\end{tikzpicture}
\end{document}
  • 1
    This and your follow up question are a really great improvement over the original one you deleted. I'm glad to see that you thought better of your earlier response and posted the code you were struggling with. As you see, it is a lot easier for people to help in that case. Just for future reference: you could have edited your original question rather than deleting it and starting over. There is an edit button at the bottom left of your post. (Editing will put your question back on the front page in the same way as asking a new question.) – cfr Jun 6 '14 at 21:07
1

Option where n children is the correct choice. However, setting grow changes the growth direction of child nodes, not the node itself. Thus:

where n children=0{draw=red,for parent={grow'=120}}{},

(grow' reverses the order of children.)

It goes without saying that further customisation will be needed to achieve the result shown in the non-forest example below. As it is, the child nodes of N1 and N2 are grown in the desired direction, but placed to the right of the nodes, not below them ...

  • It does not look like grow is the solution. The MWE is very simplistic, but in reality I need to create the charts with many nodes at the lowest level. Thus where n children=0 would be the widest level and grow would simply make matters worse. Adding 7-8 lowermost nodes produces undreadable tree with grow=-45 :( – ajeh Jun 6 '14 at 15:04
  • Do you think it would be possible to fit the lowermost children into a band or grow=east and lower them all below their parents? – ajeh Jun 6 '14 at 15:24
  • This is a perfect answer that put me on the right track with only some minor tweaking left, for which I posted a follow-up question at tex.stackexchange.com/q/183581/37570 – ajeh Jun 6 '14 at 17:53

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.