4

I'm trying to draw a derivation tree for a simple grammar using tikz-qtree.

Here is my current code:

\documentclass[11pt]{article} \usepackage{tikz} \usetikzlibrary{shapes,arrows,calc} \usepackage{tikz-qtree} % ---------- \begin{document} \begin{tikzpicture} \tikzset{grow'=right} \tikzset{every tree node/.style={anchor=base west}} \Tree [.$SS$ [.$(S)$ $()$] [.$(S)$ [.$((S))$ $(())$] ] ] \end{tikzpicture} \end{document} 

My intended result looks like this:

 -(S)------------() -/ -/ -/ -/ -/ SS -\ -\ -\ -\ -\ -\ -(S)---------((S))----------(()) 

When I compile the document, I get a bunch of missing end brace complaints "./main.tex:21: Missing } inserted", but the square brackets in the \Tree are balanced as far as I can see.

What am I doing wrong?

2 Answers 2

7

Here are three different ways to draw the graph

tikz-qtree solution

Take care of the white space!

\documentclass[11pt]{article} \usepackage{tikz} \usetikzlibrary{shapes,arrows,calc} \usepackage{tikz-qtree} % ---------- \begin{document} \begin{tikzpicture} \tikzset{grow'=right} \tikzset{every tree node/.style={anchor=base west}} \Tree [.$SS$ [.$(S)$ $()$ ] [.$(S)$ [.$((S))$ $(())$ ] ] ] \end{tikzpicture} \end{document} 

enter image description here

Pure TikZ solution

\documentclass[tikz]{standalone} \usetikzlibrary{positioning} \begin{document} \begin{tikzpicture}[node distance=0.5cm] \node (ss) {$SS$}; \node[above right=of ss] (sa) {$(S)$}; \node[right=of sa] (br1) {$()$}; \node[below right=of ss] (sb) {$(S)$}; \node[right=of sb] (ssb) {$((S))$}; \node[right=of ssb] (br2) {$(())$}; \draw (br1) -- (sa) -- (ss) -- (sb) -- (ssb) -- (br2); \end{tikzpicture} \end{document} 

enter image description here

tikzcd solution

\documentclass{standalone} \usepackage{tikz-cd} \begin{document} \begin{tikzcd}[every arrow/.append style={dash}] & (S) \arrow[r] & ()\\ SS \arrow[ru]\arrow[rd] & &\\ & (S) \arrow[r] & ((S)) \arrow[r] & (()) \end{tikzcd} \end{document} 

enter image description here

3
  • 1
    Can you point out the difference? What did I mess up? Commented Apr 13, 2019 at 17:33
  • 1
    @AdamWilliams There are difference: my [.$(S)$ $()$ ] is working but your [.$(S)$ $()$] is not working (take note of the white space) Commented Apr 13, 2019 at 17:34
  • 1
    @AdamWilliams You are welcome! Although you were so close, but only one white space is a great deal for this package :) Unfortunately, the error message is not really clear. Commented Apr 13, 2019 at 17:49
7

I'd like to persuade you to switch to forest.

\documentclass{article} \usepackage[edges]{forest} \begin{document} \begin{forest} for tree={grow=east} [$SS$ [$(S)$ $()$] [$(S)$ [$((S))$ $(())$] ] ] \end{forest} \end{document} 

enter image description here

1
  • A tempting proposition - I do prefer forest's syntax! Commented Apr 13, 2019 at 17:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.