# Restoring qtree spacing in tikz-qtree

Is it somehow possible to achieve the same node spacing in tikz-qtree as in the original qtree package? Compare the two:

qtree:

tikz-qtree:

The spacing between nodes in one level should be the same. Also, I somehow dislike that the lengths of the edges are depending on the content (see the dot vs. Z).

The reason I use tikz-qtree is because I have a tree which has a node with more than 5 branches, which qtree doesn't support.

Edit: And the tree code:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tikz-qtree}
% or \usepackage{qtree}

\newcommand{\bftt}[1]{\textbf{\texttt{#1}}}
\newcommand{\ts}[1]{\bftt{#1}}

\begin{document}

\Tree [.$Z$ \ts{(} [.$Z$ \ts{.} [.$Z$ \ts{(} [.$Z$ $\epsilon$ ] \ts{)} [.$Z$ $\epsilon$ ] ] ] \ts{)} [.$Z$ $\epsilon$ ] ]

\end{document}

-
Can you post a document with the tree so we can play? –  Alan Munn Mar 4 '13 at 13:52
Oh, of course, totally forgot that. –  neo Mar 4 '13 at 13:55
\tikzset{sibling distance=2pc}, for instance –  egreg Mar 4 '13 at 14:02
That's not working, tikz-qtree still adjusts the spacing. It just gets wider in total. –  neo Mar 4 '13 at 14:07

It's hard to replicate qtree spacing in tikz-qtree since the latter package uses a completely different method for node spacing; most of the time this leads to much better looking trees, especially for linguistics, which was its intended use. You can balance out particular node manually by enclosing their contents in an explicit \node command and specifiying a width. The imbalance comes from having very different widths dominated by by the nodes at the first level. See How do I center this qtree? for a similar problem. For the node size problem, I've used egreg's \phantom solution.

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tikz-qtree}
\tikzset{every tree node/.style={align=center,anchor=north}}

\newcommand{\bftt}[1]{\textbf{\texttt{#1}}}
\newcommand{\ts}[1]{\bftt{#1}\protect\vphantom{$A$}}

\begin{document}
\begin{tikzpicture}[]
\Tree [.$Z$ \node[minimum width=1cm]{\ts{(}}; [.$Z$ \ts{.} [.$Z$ \ts{(}
[.$Z$ $\epsilon$ ] \ts{)} [.$Z$ $\epsilon$ ] ] ] \node[minimum width=2cm]
{\ts{)}}; [.$Z$ $\epsilon$ ] ]
\end{tikzpicture}
\end{document}


## A solution using the forest package

The recent forest package provides the tools to do this fairly easily. Although its main raison d'être is to make more compact trees, it provides hooks to make less compact trees too. The input syntax is a bit different: there are no . to mark node labels, and terminal nodes must be enclosed in [ ... ].

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usepackage{forest}
\newcommand{\bftt}[1]{\textbf{\texttt{#1}}}
\newcommand{\ts}[1]{\bftt{#1}\protect\vphantom{$A$}}

\begin{document}
\forestset{qtree edges/.style={for tree={
parent anchor=south, child anchor=north}}}

\begin{forest}
[$Z$, qtree edges,s sep=1cm [\ts{(}] [$Z$ [\ts{.}] [$Z$ [\ts{(}] [$Z$ [$\epsilon$] ]
[\ts{)}] [$Z$ [$\epsilon$] ] ] ] [\ts{)}] [$Z$ [$\epsilon$] ] ]
\end{forest}
\end{document}


-
Ok, but I really don't want to do this manually for tens of trees. anchor=north seems to place the dot too high. –  neo Mar 4 '13 at 14:44
@neo On the centering issue of the dot, try anchor=center then. –  Alan Munn Mar 4 '13 at 14:47
But then the original problem with the different edge lengths reappears. egreg posted a workaround for this using phantom. –  neo Mar 4 '13 at 14:59
The main problem with tikz-qtree is the case of three branches. Then, often the middle edge isn't straight and doesn't look right. Regarding forest, the leaves shouldn't overlap each other, so that's not really what I'm looking for. –  neo Mar 4 '13 at 16:18
I was a bit busy in the last days, so @AlanMunn beat me with the answer using forest --- thanks Alan! I'd like to add that I'm just developing (yet) another hook which will make it relatively easy to implement even spacing (among other things). It should be ready in a week or so. I'll post an answer when it is. –  Sašo Živanović Mar 5 '13 at 15:29

The documentation of tikz-qtree mentions the key sibling distance; you can set it globally with, say,

\tikzset{sibling distance=1pc}


in the preamble or locally for a particular tree with

\begin{tikzpicture}[sibling distance=1pc]
\Tree [...]
\end{tikzpicture}


For the height of the nodes, I suggest to put a phantom in the definition of \ts:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz-qtree}

\newcommand{\bftt}[1]{\textbf{\texttt{#1}}}
\newcommand{\ts}[1]{\bftt{#1}\protect\vphantom{$A$}}

\begin{document}

\begin{tikzpicture}[sibling distance=1pc]
\Tree [.$Z$ \ts{(} [.$Z$ \ts{.} [.$Z$ \ts{(} [.$Z$ $\epsilon$ ] \ts{)}
[.$Z$ $\epsilon$ ] ] ] \ts{)} [.$Z$ $\epsilon$ ] ]
\end{tikzpicture}
\end{document}


-
I like the phantom, but the spacing is still different? In qtree you can see that all four nodes of level 1 are exactly separated by the same amount of space. –  neo Mar 4 '13 at 14:41