# How to draw syntactical trees with parallel leafs for a natural language?

I need to draw syntactical trees for a natural language. The problem is that I need all the leafs to be parallel. How the other nodes are sorted in relation to the other at the same depth is not important. In practical use you would a sentence at the bottom of a paper, both sideways and horizontal would be nice. The root would be at the opposite side so the tree uses the entire page. Any suggestions of a package that can do this?

Edit: Any tree package could possibly be used as long as it can use an entire page. I think.

-
Have you looked at tikz-qtree? Do you have an example you're aiming to imitate? – Matthew Leingang May 6 '11 at 18:25

This is not commonly the way trees are represented in the linguistic literature, so none of the regular tree drawing packages (qtree and tikz-qtree) do this by default.

Specifically, the way trees are drawn in the linguistics literature, the terminal nodes are not drawn at the same level (i.e. with the words along a baseline and the tree growing up from there.) So to take a simple tree of e.g John thinks he loves Mary, we could use the tikz-qtree to draw the tree easily:

\documentclass{article}
\usepackage{tikz-qtree,tikz-qtree-compat}
\begin{document}
\Tree [.S [.NP John ]
[.VP [.V\0 thinks ]
[.S [.NP he ]
[.VP [.V\0 loves ]  [.NP Mary ]]]]]
\end{document}


Notice that the terminal nodes of the tree don't line up with the other terminal nodes along a baseline. This is normal in the linguistics literature, although often when teaching students how to draw trees by hand we start with all the words lined up at the bottom (i.e. at the same level as the lowest word in the tree.)

A new solution using tikz-qtree

Starting from version 1.2 of tikz-qtree there is now a simple way to have terminal nodes line up, although choosing the value requires some manual calculation. The frontier key allows you to specify the placement of terminal nodes using the distance from root parameter. So in the example above we can make distance from root equal to 5cm and the terminal nodes will line up. This value would need to be changed for each individual tree.

\documentclass{article}
\usepackage{tikz-qtree,tikz-qtree-compat}
\begin{document}
\begin{tikzpicture}[frontier/.style={distance from root=5cm}]
\Tree [.S [.NP John ]
[.VP [.V\0 thinks ]
[.S [.NP he ]
[.VP [.V\0 loves ]  [.NP Mary ]]]]]
\end{tikzpicture}
\end{document}


Another simple solution

As an alternative to the frontier key solution above, kgr's very simple answer using regular TikZ methods can also be applied using tikz-qtree. All that is needed is to insert extra brackets in the tree to make dummy nodes. So you can simply do the following:

\begin{tikzpicture}
\Tree [.S [.NP [[[ John ]]] ]
[.VP [.V\0 [[ thinks ]] ]
[.S [.NP [ he ] ]
[.VP [.V\0 loves ]  [.NP Mary ]]]]]
\end{tikzpicture}


The only difference between the tree above and this tree is that the terminals have been enclosed in extra brackets: three for John, two for thinks and one for he. For the occasional tree this might be a simple way to achieve what you want.

An old solution using xyling

The xyling package also has a way to do this, although its input method for tree drawing is much more cumbersome. Here's the same tree formatted using xyling: (I've left this part of the answer in for posterity; the tikz-qtree answers are really much simpler.)

% compile with latex + dvips
\documentclass{article}
\usepackage{xyling}
\begin{document}
\Tree{  &       \K{S}\B{dl}\B{dr}\\
\K{NP}\B{d} &   &   \K{VP}\B{dl}\B{dr}\\
\B[6]{d}    &   \K{V}\B{d}  &   &   \K{S}\B{dl}\B{dr}\\
\B[6]{d}    &   \B[6]{d}&   \K{NP}\B{d} &   &   \K{VP}\B{dl}\B{dr}\\
\B[6]{d}    &   \B[6]{d}&\B[6]{d}   &   \K{V}\B{d}  &   &   \K{NP}\B{d}\\
John        &   thinks  &   he  &       loves   &   &   Mary    }

\end{document}


Basically, xyling sets a kind of tabular grid to draw trees. This allows you to put all the terminal nodes in the lowest row of the grid and then add explicit branches in the empty cells above it up to its non-terminal node. Since the default branch drawing command assumes a node label you need to use the optional argument of the \B command to raise the starting points of the extra branches. A value of 6 seems to do an adequate job.

xyling uses Postscript specials and will not work out of the box with pdflatex, so you need to compile this file with latex+dvips+ps2pdf The documentation explains how to modify the style file to change this (why it's not a package option I don't know.) In fact, given that there is now pdf support for much of the xy package upon which xy-ling is based, it would be simple to modify the style file to use either pdflatex or latex+dvips.

-
But you said in the comment above that tikz-qtree isn't suitable for what I want? – Algific May 7 '11 at 9:46
See my revised answer. You seemed so set on having the terminal nodes line up, that I focused my answer around that. However, if this is not actually a requirement, (but just the way you though the trees should be drawn, then the tikz-qtree package is definitely the best solution.) – Alan Munn May 7 '11 at 13:20

I would advice to use the TikZ package. Have a look in the documentation , pp. 475 for "normal trees", and pp. 386, for "mindmap-like trees".

-
I would like to see how the tikz tree methods can actually construct the tree as described by Algific. If not, then existing tree packages for drawing linguistic trees are far better for this purpose than tikz alone. – Alan Munn May 6 '11 at 19:15
You have some example here. With some adaptation in the code, the kind of trees @Algific wants are feasible. – Speredenn May 6 '11 at 19:31
I do agree with @Alan; I didn't know about the qtree and xyling packages, with what I see in their manuals, that's certainly the best solutions for the @Algific problem. – Speredenn May 6 '11 at 19:36
However, I would recommend qtree, as it seems it works with pdflatex while xyling don't. – Speredenn May 6 '11 at 19:42
@Speredenn Being a syntactician, I draw trees a lot and am quite familiar with all of the methods for tree drawing, including TikZ. I don't think TiKZ tree methods (including tikz-qtree or plain qtree) can replicate what Algific wants, since they depend on the concept of a hierarchical level. Assuming a tree of n levels, Algific wants the terminal nodes of nodes at level k < n to be at level n. I don't think TiKZ can do this without a lot of hacking. – Alan Munn May 6 '11 at 19:47

This is actually quite easy to do in tikz so long as you don't mess with node heights on an individual basis. (So mixing multiline and single-line nodes in the same tree will break this). (see comment.) Just add extra childs:

\begin{tikzpicture}
[sibling distance=2.5cm,level distance=1.2cm,edge from parent   path={(\tikzparentnode.south) --
(\tikzchildnode.north)}, inner sep=1.5pt]
\node {S}
child {node {NP}
child {child {child {child {node {\textit{John}}}}}}}
child {node {VP}
child {node {V}
child {child {child {node {\textit{thinks}}}}}}
child {node {S}
child {node {NP}
child {child {node {\textit{he}}}}}
child {node {VP}
child {node {V}
child {node {\textit{loves}}}}
child {node {NP}
child {node {\textit{Mary}}}}}}};
\end{tikzpicture}

-
Actually, I'm wrong! I tested a multiline node created using "text width" at a small value and it still works, guess in an earlier test I had "anchor" set oddly. – kgr May 10 '11 at 18:17
Thanks! This idea will also work with tikz-qtree but with a simpler syntax. I never thought of doing it this way. I'll add to my answer based on your idea. – Alan Munn May 10 '11 at 18:32
great, yeah I was wondering if this would extend to tikz-qtree. (I don't really use it myself, so wasn't sure what the right syntax to try would be.) – kgr May 10 '11 at 18:52

For completeness, the extremely powerful forest package should be included ;):

\documentclass[standalone, border=5pt]{standalone}
\usepackage{forest}
\newcommand*\primary{\textsuperscript{0}}
\begin{document}
\begin{forest}
for tree={
where n children=0{tier=terminal}{},
parent anchor=south,
child anchor=north,
}
[S
[NP
[John
]
]
[VP
[V\primary
[thinks
]
]
[S
[NP
[he
]
]
[VP
[V\primary
[loves
]
]
[NP
[Mary
]
]
]
]
]
]
\end{forest}
\end{document}


tier is doing the work of keeping the final nodes aligned. where n children=0{}{} is applying this to the terminal nodes (which have no children).

-