Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Sure, now it's a complete minimal document :) –  Juan A. Navarro Nov 21 '12 at 19:20
add comment

1 Answer

up vote 7 down vote accepted

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

share|improve this answer
    
Amazing! It does work! I worried that this might require messing with the tree drawing internals, but it's great to see that changing a few simple options can do the trick! Thanks! –  Juan A. Navarro Nov 22 '12 at 10:37
    
Ah! A small addition, I realised that while the nodes are place appropriately, edges are still being drawn from the centers of nodes, this becomes obvious when branching. However adding the options parent anchor={south},child anchor={north} cause the edges to be drawn from the parents bottom to the children top. –  Juan A. Navarro Nov 22 '12 at 11:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.