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?

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

  • 1
    Can you point out the difference? What did I mess up? – Adam Williams Apr 13 '19 at 17:33
  • 1
    @AdamWilliams There are difference: my [.$(S)$ $()$ ] is working but your [.$(S)$ $()$] is not working (take note of the white space) – user156344 Apr 13 '19 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. – user156344 Apr 13 '19 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

  • A tempting proposition - I do prefer forest's syntax! – Adam Williams Apr 13 '19 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.