11

I would like to draw a syntax tree, which resembles the below syntax tree. I know that I can use tikz to draw syntax trees, but I can't seem to figure out how to draw a straight line down to a node, which is placed right below the parent node.

\begin{tikzpicture}
\node{S}
 child {node {a}}
 child {node {B}};
\end{tikzpicture}

The syntax tree I want to draw

3
  • 1
    Can you add a your latex code showing what you have tried. It is often easier to help when you can just modify a code instead of starting from scratch.
    – mythealias
    Dec 1, 2012 at 19:54
  • 1
    I've added the code I have to far :) Dec 1, 2012 at 20:12
  • An example from texample.net/tikz/examples/feature/trees might help you get started.
    – N.N.
    Dec 1, 2012 at 20:14

3 Answers 3

11

Here you have a starting point:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}[
  tlabel/.style={pos=0.4,right=-1pt,font=\footnotesize\color{red!70!black}},
]
\node{S}
child {node {a}}
child {node {S} 
  child {node {a}}
  child {node {S}
    child {node {$\varepsilon$}
      edge from parent node[tlabel,pos=0.2] {2}
    }
    edge from parent node[tlabel] {1}
  }
  child {node {B}
    child {node {B}
      child {node {b}
        edge from parent node[tlabel,pos=0.2] {5}
     }
      edge from parent node[tlabel] {4}
    }
    child {node {b}}
  }
    edge from parent node[tlabel] {1}
  }
child {node {B}
  child[missing] {}
  child[missing] {}
  child {node {b}
    edge from parent node[tlabel,pos=0.15,right=2pt] {5}
  }
};
\end{tikzpicture}

\end{document}

enter image description here

11

This is example with some different decoration, but can be easyly adapted:

MWE

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{fullpage}
\usepackage{calc}
\usetikzlibrary{positioning,shadows,arrows,trees,shapes,fit}


\begin{document}

\begin{tikzpicture}
[font=\small, edge from parent, 
    every node/.style={top color=white, bottom color=blue!25, 
    rectangle,rounded corners, minimum size=6mm, draw=blue!75,
    very thick, drop shadow, align=center},
    edge from parent/.style={draw=blue!50,thick},
    level 1/.style={sibling distance=3cm},
    level 2/.style={sibling distance=1.2cm}, 
    level 3/.style={sibling distance=1cm}, 
    level distance=2cm,
    ]
    \node (A) {A} 
        child { node (B) {B}
            child { node {B1} 
            edge from parent node[left=.5em,draw=none] {$\chi^2$} }
            child { node {B2}}
            }
        child {node (C) {C}
            child { node {C1}
                child { node {C1a}}
            }
        child { node {C2}}
        child { node {C3}
            child { node {C3a}}
            child { node {C3b} edge from parent node[right=.5em,draw=none] {$\frac{a}{b}$}}
            }
        }
    child { node {D} 
        child { node {D1}}
        child { node {D2}}
};

\end{tikzpicture}
\end{document}
4

The tikz-qtree package provides a simpler syntax for drawing these kinds of trees generally. Annotation of edges proceeds in a similar fashion.

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[%
  sibling distance=.5cm,
  empty/.style={draw=none},
  tlabel/.style={font=\footnotesize\color{red!70!black}}]
\Tree  [.S  
         [.a ]
         \edge node[tlabel,auto=left] {1}; 
         [.S  
             [.a ] 
            \edge node[tlabel,auto=left] {1};
            [.S \edge node[tlabel,auto=left] {2}; [.$\epsilon$ ] ] 
            [.B \edge node[tlabel,auto=right] {4};
              [.B \edge node[tlabel,auto=left] {5}; [.b ]]
              [.b ]
            ]
         ] 
         [.B   \edge[empty]; {} \edge node[tlabel,auto=left] {5}; {b}   ]
       ]

\end{tikzpicture}
\end{document}

output of code

2
  • Do you know how to integrate qtree into the editor tikzedt? The code above does not compile even if I add \usepackage{tikz-qtree} to the preamble section.
    – Paddre
    Dec 29, 2017 at 15:44
  • @Paddre sorry no. I've never used tikzedt.
    – Alan Munn
    Dec 29, 2017 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .