Trees in latex : Depth of 4 nodes

I am following the current tutorial (MWE) : link

However, I am facing difficulties to perform a tree of 4 nodes. Items are superposed, and even if I add a child [missing] {} it doesn't solve the issue.

On below an example of code which is not displayed correctly :

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {texmf}
child { node {doc}}
child { node {fonts}}
child { node {source}}
child { node [selected] {tex}
child { node {generic}}
child {
node [optional] {latex}
child { node {latex21} }
child { node {latex22} }
child { node {latex23} }
}
child [missing] {}
child [missing] {}
child [missing] {}
child {
node {plain}
child { node {plain21} }
child { node {plain22} }
child { node {plain23} }
}
child [missing] {}
child [missing] {}
child [missing] {}
}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child { node {texdoc}};
\end{tikzpicture}
\end{document}


Actually, I do not understand where and when to use child [missing] {}. Thank you in advance for your help.

-
The following question might be useful: (possibly a duplicate) Drawing a directory listing a la the tree command in TikZ. Also, there is the dirtree package, which doesn't use tikz: How to draw a hierarchical structure with edges?. For an alternative tree input syntax, (much easier) you can also use tikz-qtree. See Horizontal hierarchy tree in tikz-qtree: bad layout for longer node-names for an example like yours. –  Alan Munn Jul 25 '13 at 15:15
@AlanMunn : Yes I've already seen this post. However I do like having the boxes, and I rather like this style. Thanks –  Header12 Jul 25 '13 at 15:18
@Header12 Politely asking you to respect the policy on this site is neither ironic, nor unwarranted criticism. This was no personal attack, just a constructive suggestion with no irony present. If you are willing to use this site, you might as well try to follow its rules. I removed the comments since you were kind enough to improve on your question. I also wish you good luck and refer you to my provided answer. –  MHaaZ Jul 25 '13 at 15:29
@Header12 Just to explain why we like actual code rather than links: links die over time, so all Stack Exchange sites discourage links that are not within the Stack Exchange domain, especially as they pertain to the main content of a question or answer. –  Alan Munn Jul 25 '13 at 15:33
You both got points. I really thought that @MHaaZ was trolling me. I now understand. Thank you for the help, and sorry for my having react in that way. –  Header12 Jul 25 '13 at 15:36

You should add 3 more child[missing] if you want the texdoc node to be "on its own" horizontally.

Tikz actually tries to grow balanced trees, so for drawing a directory like this, a little bit of "magic" is necessary: the missing nodes. For further insight into how missing nodes work, you could take a look into the the pgf/TikZ manual. Basically TikZ draws trees without consideration of node sizes, and thus with this code it is necessary to "skip" some nodes (mark them as missing) to get the correct placement. For two nodes a and b at level 2, you should insert as many missing nodes inbetween as there are descendants of a (children and grandchildren and so on...). This is to make the tree "skip" as many nodes as necessary to put b in its right place. In your case, there are 6 leaf nodes (level 4) and 3 middle nodes (level 3), so you need to "skip" 9 nodes. The code below:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {texmf}
child { node {doc}}
child { node {fonts}}
child { node {source}}
child { node [selected] {tex}
child { node {generic}}
child {
node [optional] {latex}
child { node {latex21} }
child { node {latex22} }
child { node {latex23} }
}
child [missing] {}
child [missing] {}
child [missing] {}
child {
node {plain}
child { node {plain21} }
child { node {plain22} }
child { node {plain23} }
}
child [missing] {}
child [missing] {}
child [missing] {}
}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child [missing] {}
child { node {texdoc}};
\end{tikzpicture}
\end{document}


yields the following image, which hopefully is what you wanted:

-
Thanks for the explanations. Working great so far, I now understand. Regards. –  Header12 Jul 25 '13 at 15:39