[Tex/LaTex] Same node distance between levels in a tikz tree

tikz-pgftikz-trees

I want to draw a tree using tikz, but I want the vertical distance between nodes in successive levels to be the same, regardless of the size of the node.

For example:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[level distance=1cm]
\path node {a}
      child {
        node {b}
        child {
          node [align=center] {c \\ d}
          child {
            node {e}
          }
        }
      };
\end{tikzpicture}
\end{document}

I know this is a silly example of a tree, since it doesn't “branch”, but it shows better the problem I'm referring to. The actual tree that I have to produce does eventually branch.
But anyway, the produced output looks like:

enter image description here

where the edge between a—b is longer than the edge between b-cd.

What I want is the distance between the bottom of a parent node and the top of its children nodes to remain constant throughout the tree, regardless of the (vertical) size of the nodes. In this example all edges should show up with the same size.

Is there a way to easily accomplish this using tikz?

Best Answer

Tikz builds the tree in such a way that the length level distance is the distance between the point growth parent anchor and the anchor of the child.

By default, the growth parent anchor is center. And the anchor of the children are the default nodes anchors, which are also center. So you get 1cm between the centers of the nodes.

In order to have 1cm between the edges of the nodes as you request, you have to set growth parent anchor to south, and set also the default anchor for all the nodes of the tree to north. The following code implements this idea (and adds some blue lines to test if it works).

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[level distance=1cm, growth parent anchor={south}, nodes={anchor=north}]
\path node (a) {a}
      child {
        node (b) {b}
        child {
          node [align=center] (cd) {c \\ d}
          child {
            node (e) {e}
          }
        }
      };
  % Testing
  \foreach \n in {a,b,cd} {
     \draw[blue,<->] (\n.south)  ++(2mm,0) -- ++(0, -1cm) node[midway, right]{1cm};
  }
\end{tikzpicture}
\end{document}

It works! See the result:

    Output