Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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 '12 at 19:54
1  
I've added the code I have to far :) –  Niels Sønderbæk Dec 1 '12 at 20:12
    
An example from texample.net/tikz/examples/feature/trees might help you get started. –  N.N. Dec 1 '12 at 20:14
add comment

3 Answers 3

up vote 7 down vote accepted

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

share|improve this answer
add comment

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}
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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