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 am trying to draw a tree using the tikz-qtree package, however I am having issues with the nodes of the tree being not centered.

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{tikz-qtree}
\begin{document}

\begin{tikzpicture}
\node{\Tree 
 [.{ABC} 
    [.\textbf{D}
        [.AA BB ] ]
    [.\textbf{E}
        [.CC DD ]
        [.EEEEEE FF ] ]
    [.\textbf{F}
        [.GG HH ] ]
    ]};\\

\end{tikzpicture}

\end{document}

Which produces the following Output:

Screenshot

How do I get the tree to center its nodes correctly? I have tried playing with some options of the package, such as

[level 1/.style={node distance=40mm}]

This, however, didn't yield any result and left the tree unchanged. Does anyone have an idea on how to fix this?

share|improve this question
    
This is by design. The package tries to minimize the total width of the tree. If you centred node E, you would have to move F further to the right, making the whole tree bigger. –  Alan Munn Feb 13 '12 at 23:01
    
Thank you. Is there a way to center node E or does this package simply not offer such a functionality? –  Julian Feb 13 '12 at 23:04
1  
Even if you make the nodes further apart with [sibling distance=30pt] (as an example) it will still give you an angled middle branch. This way the space between AA/CC is equivalent to the space between EEEEEE/GG. I'll post a manual solution in a few minutes. –  Alan Munn Feb 13 '12 at 23:07
add comment

2 Answers

up vote 16 down vote accepted

The spacing in the tree is by design. The package tries to minimize the total width of the tree. If you centred node E, you would have to move F further to the right, making the whole tree bigger. Even if you make the nodes further apart with [sibling distance=30pt] (as an example) it will still give you an angled middle branch. This way the space between AA/CC is equivalent to the space between EEEEEE/GG.

If you truly want a vertical middle bar, you can manually adjust the node that is causing the problem (in this case the imbalance between CC and EEEEEE). But this will make the space between AA/CC and EEEEEE/GG no longer equal. So either way you end up with some asymmetry in the tree.

I also removed some unnecessary code from your example. The whole tree doesn't need to be in a \node and you don't need to load the tikzlibrary{trees} to use tikz-qtree (it uses low level pgf methods for tree construction.)

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
% no adjustment
  \Tree 
 [.{ABC} 
    [.\textbf{D}
        [.AA BB ] ]
    [.\textbf{E}
        [.CC DD ]
        [.EEEEEE FF ] ]
    [.\textbf{F}
        [.GG HH ] ]
  ]

% Manually adjusted tree
\begin{scope}[xshift=3in]
\Tree 
 [.{ABC} 
    [.\textbf{D}
        [.AA BB ] ]
    [.\textbf{E}
        [.\node[minimum width=4.75em] {CC}; DD ]
        [.EEEEEE FF ] ]
    [.\textbf{F}
        [.GG HH ] ]
  ]
\end{scope}
\end{tikzpicture}

\end{document}

output of code

share|improve this answer
    
Thank you very much! I will have a look into your answer tomorrow as I'm currently not at a PC, but as far as I can tell your proposed solution is exactely what I was looking for! :) –  Julian Feb 13 '12 at 23:37
3  
God I love this website. –  Todd Lehman Feb 14 '12 at 2:24
add comment

with automatic adjustment

\documentclass{article}
\usepackage{tikz-qtree}
\usepackage{calc}    
\begin{document}

\begin{tikzpicture}
  \Tree 
 [.{ABC} 
    [.\textbf{D}
        [.AA BB ] ]
    [.\textbf{E}
        [.\makebox[\widthof{EEEEEE}]{CC} DD ]
        [.EEEEEE FF ] ]
    [.\textbf{F}
        [.GG HH ] ]
  ]
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
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.