[Tex/LaTex] Restoring qtree spacing in tikz-qtree

qtreespacingtikz-qtreetrees

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

qtree:

qtree

tikz-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}

Best Answer

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}

output of tikz-qtree code

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}

enter image description here

Related Question