# Syntactic tree with tikz-qtree and annotations and labels over multiple lines [duplicate]

I am going mad trying to draw syntactic trees using the tikz-qtree package. Here is what I have come up with:

\begin{figure}
\centering
\begin{tikzpicture}[every node/.style={align=center},level distance=1.5cm]
\Tree [.S
[.{NP\\($\uparrow$~{\sc subj})~=~$\downarrow$}
[.{N\\$\uparrow$~=~$\downarrow$}
[. John
]
]
]
[.{VP\\$\uparrow$~=~$\downarrow$}
[.V
[.{saw\\$\uparrow$~=~$\downarrow$}]
]
[.{NP\\($\uparrow$~{\sc obj})~=~$\downarrow$}
[.{DET\\($\uparrow$~{\sc def})~=~+}
]
[.{N\\$\uparrow$~=~$\downarrow$}
[. boy
]
]
]
]
]
\end{tikzpicture}
\caption{test}
\end{figure}


However, tex keeps complaining about a runaway argument, and I can't seem to be able to spot what I'm doing wrong. It seems to me that I need to use curly braces because the labels on the tree span multiple lines. Any ideas?

• Please post complete code, starting with \documentclass and ending with \end{document}. – cfr Jan 7 '15 at 21:30

You've run into a pretty common error for users of qtree/tikz-qtree: the package syntax requires that all closing brackets must be preceded by whitespace. If you add a space (or a line break, as Jesse's answer does) before the bracket that closes the saw node, your MWE compiles without errors:

\documentclass{article}

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

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[every node/.style={align=center},level distance=1.5cm]
\Tree [.S
[.{NP\\($\uparrow$~{\sc subj})~=~$\downarrow$}
[.{N\\$\uparrow$~=~$\downarrow$}
[. John
]
]
]
[.{VP\\$\uparrow$~=~$\downarrow$}
[.V
[.{saw\\$\uparrow$~=~$\downarrow$} ] % the space before the closing bracket is crucial
]
[.{NP\\($\uparrow$~{\sc obj})~=~$\downarrow$}
[.{DET\\($\uparrow$~{\sc def})~=~+}
]
[.{N\\$\uparrow$~=~$\downarrow$}
[. boy
]
]
]
]
]
\end{tikzpicture}
\caption{test}
\end{figure}

\end{document}


## EDIT

• Changed the deprecated \sc to \textsc{} as suggested by cfr
• Changed every node to every tree node and added anchor=north so that the top of the V node is vertically aligned with the top of the NP node
• Limited the level distance increase to the 2nd level and lower so that there isn't too much vertical space between the S and its daughters

\documentclass{article}

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

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[every tree node/.style={align=center,anchor=north},level 2+/.style={level distance=1.5cm}] % changed "every node" to "every tree node" and added "anchor=north" so that the top of the V node is vertically aligned with the top of the NP node; increased the level distance for only the 2nd level and lower
\Tree [.S
[.{NP\\($\uparrow$~\textsc{subj})~=~$\downarrow$} % changed \sc to \textsc{}
[.{N\\$\uparrow$~=~$\downarrow$}
[. John
]
]
]
[.{VP\\$\uparrow$~=~$\downarrow$}
[.V
[.{saw\\$\uparrow$~=~$\downarrow$} ]
]
[.{NP\\($\uparrow$~\textsc{obj})~=~$\downarrow$} % changed \sc to \textsc{}
[.{DET\\($\uparrow$~\textsc{def})~=~+} % changed \sc to \textsc{}
]
[.{N\\$\uparrow$~=~$\downarrow$}
[. boy
]
]
]
]
]
\end{tikzpicture}
\caption{test}
\end{figure}

\end{document}


• \sc was deprecated 20+ years ago. It is from LaTeX 2.09. The 2e commands are \scshape or \textsc{}. – cfr Jan 7 '15 at 21:29
• @cfr, yes, good point. Sorry I missed that in the MWE. – Jason Zentz Jan 7 '15 at 21:30
• (+1) Easy to miss. Good explanation of Jesse's answer. – cfr Jan 7 '15 at 21:41

This is a result after debugging. The errors found are indicated by <---.

Edit: There are two ways of doing this. The new one is on the top while the old one follows. The new one is found after the two brackets are put together.

Code

\documentclass[]{standalone}
\usepackage{graphicx}
\usepackage{tikz-qtree}
\usetikzlibrary{trees}
\begin{document}
%\begin{figure}

new solution

\centering
\begin{tikzpicture}[every node/.style={align=center},level distance=1.5cm]
\Tree [.S
[.{NP\\($\uparrow$~{\scshape subj})~=~$\downarrow$}
[.{\strut N\\$\uparrow$~=~$\downarrow$}
[. John
]
]
]
[.{VP\\$\uparrow$~=~$\downarrow$}
[.V
[.{ saw\\$\uparrow$~=~$\downarrow$}
]] % <---  this is how it work,
[.{NP\\($\uparrow$~{\scshape obj})~=~$\downarrow$}
[.{DET\\($\uparrow$~{\scshape def})~=~+}
]
[.{N\\$\uparrow$~=~$\downarrow$}
[. boy
]
]
]
]
]
\end{tikzpicture}

% Old solution
\centering
\begin{tikzpicture}[every node/.style={align=center},level distance=1.5cm]
\Tree [.S
[.{NP\\($\uparrow$~{\scshape subj})~=~$\downarrow$}
[.{N\\$\uparrow$~=~$\downarrow$}
[. John
]
]
]
[.{VP\\$\uparrow$~=~$\downarrow$}
[.V
[.{saw\\$\uparrow$~=~$\downarrow$} % <---
]
[.{NP\\($\uparrow$~{\scshape obj})~=~$\downarrow$}
[.{DET\\($\uparrow$~{\scshape def})~=~+}
]
[.{N\\$\uparrow$~=~$\downarrow$}
[. boy
]
]
]
]
]
]   %<---
\end{tikzpicture}
%\caption{test}
%\end{figure}
\end{document}

• Thanks!! But now I'm confused as to why this works. It seems to me that the square brackets are not balanced - there is one more closing bracket in the end than there are opening brackets. Could you elaborate a bit? – Sebastian Sulger Jan 7 '15 at 17:11
• OK, you have two brackets at the end of verb saw where only one is required to close the branch. And the last bracket at the end is to close the entire sentence (S). Without the last bracket, only the verb phrase (VP) is closed which will still cause error. All the brackets are balanced. You have two branches. It is fairly clear from the structure there are NP and VP. – Jesse Jan 7 '15 at 17:34
• \sc is deprecated. – cfr Jan 7 '15 at 21:29
• I hope you don't mind - I updated the commands because I wanted to (+1) the answer. Especially for disambiguating the readings. Not sure you need graphicx? – cfr Jan 7 '15 at 21:40
• Thanks. @cfr, No need for graphicx. I copy and paste preamble from elsewhere when worked on the question. – Jesse Jan 7 '15 at 22:35

Here's a forest version which makes use of a couple of new commands to make coding the tree a bit simpler. (You could use these with the tikz-qtree solutions instead, of course.)

\upit{arg}


takes one mandatory argument, arg. It typesets an upwards arrow followed by arg in small caps, all wrapped in parentheses.

\updown{arg}


takes an optional argument, arg, but lets you use {} rather than [] so you don't have to wrap everything in curly brackets to avoid having the argument read as a new node. If no argument is specified, it typesets an upwards arrow, an equals sign and a downwards arrow. Otherwise, it typesets the arg as \upit does, adds an equals sign and finishes with a downwards arrow.

I include the code for the basic tree:

and a variant which seems to be quite common:

The only difference in code is the addition of

if n children=0{tier=terminal}{},


to the tree configuration in the second case.

\documentclass[tikz, border=5pt, varwidth, multi]{standalone}
\usepackage{forest, mathtools, xparse}
\standaloneenv{forest}
\begin{document}
\newcommand*\upit[1]{\ensuremath{(\uparrow\,\text{\scshape #1})}}
\NewDocumentCommand\updown { g }{%
\IfNoValueTF{#1}{%
\ensuremath{\uparrow\,=\,\downarrow}%
}{%
\ensuremath{(\uparrow\,\text{\scshape #1})\,=\,\downarrow}%
}}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
align=center,
base=top,
}
[S
[NP\\\updown{subj}
[N\\\updown
[John
]
]
]
[VP\\\updown
[V
[saw\\\updown
]
]
[NP\\\updown{obj}
[DET\\{$\upit{def} = +$}
[the
]
]
[N\\\updown
[boy
]
]
]
]
]
\end{forest}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
align=center,
base=top,
if n children=0{tier=terminal}{},
}
[S
[NP\\\updown{subj}
[N\\\updown
[John
]
]
]
[VP\\\updown
[V
[saw\\\updown
]
]
[NP\\\updown{obj}
[DET\\{$\upit{def} = +$}
[the
]
]
[N\\\updown
[boy
]
]
]
]
]
\end{forest}
\end{document}