7

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}

Issue of display, textdoc should be on below

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

Header12
  • 639

1 Answers1

6

Short answer:

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

Long answer:

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:

The OP's tree with the correct spacing for nodes

MaxAxeHax
  • 1,210