# Create a syntax tree with LaTeX

Is is possible to create this tree with LaTeX ?

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

\begin{document}

\begin{tikzpicture}
\node {E}
child {  node {F}   }

child {  node {+}}

child {  node {T}

child {  node {T}
child{node{F}

child{node{8}} }}

child { node {*} }

child { node {F}

child{node{2}}}};

\end{tikzpicture}

\end{document}


I managed to create the tree but I lack the attributes for example{Fv: = 2}

• There are plenty of tree examples on the site, (just clicking on the tikz-trees or tikz-qtree tags will show you lots of examples.) This one isn't particularly hard. I would recommend that you construct an example of what you have tried and then ask a more specific question based on that (for example how to align the annotations given a particular tree.) – Alan Munn Apr 30 '13 at 14:54

Adapting Gonzalo's answer to the tikz-qtree package we can achieve a similar result. Note that strictly speaking you don't need to assign \nodes to nodes which you are not referring to with other TikZ commands.

\documentclass{article}
\usepackage{tikz-qtree}
\usepackage{mathtools}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}
\Tree [.\node(a) {E};
[.\node (b) {F}; ]
[.\node (c) {+}; ]
[.\node (d) {T};
[.\node (e) {T};
[.\node (f) {F}; [.\node (g) {8}; ]]]
[.\node (h) {*}; ]
[.\node (i) {F};
[.\node (j) {2}; ]
]
]
]
\begin{scope}[every node/.style={font=\footnotesize}]
\node[right=of a] (na) {\{E.v${}\coloneqq{}$+T.v\}};
\node[right=of d] (nd) {\{T.v${}\coloneqq{}$F.v$^{*}$T.v\}};
\node[right=of i] (ni) {\{F.v${}\coloneqq{}$2\}};
\node[left=of b] (nb) {\{F.v${}\coloneqq{}$5\}};
\node[left=of e] (ne) {\{T.v${}\coloneqq{}$F.v\}};
\node[left=of f] (nf) {\{F.v${}\coloneqq{}$8\}};
\foreach \i in {a,b,d,e,f,i}
\draw[dotted] (\i) -- (n\i);
\end{scope}
\end{tikzpicture}
\end{document}


• I knew you were going to give the qtree version ;-) – Gonzalo Medina Apr 30 '13 at 17:10

You can name the nodes and then use these names to place the annotations using other \nodes and the positioning library (I wasn't sure if the annotations should be in text or math mode, but you can easily change that); a simple \foreach loop allows to draw the dotted lines:

\documentclass{scrartcl}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{trees,positioning}

\begin{document}

\begin{tikzpicture}
\node (a) {E}
child {node (b) {F}
}
child {node (c) {+}
}
child {node (d) {T}
child {node (e) {T}
child {node (f) {F}  child {node  (g) {8}} }
}
child {node  (h) {*}}
child {node  (i) {F} child {node  (j) {2}}}
};
\begin{scope}[every node/.style={font=\footnotesize}]
\node[right=of a] (na) {\{E.v${}\coloneqq{}$+T.v\}};
\node[right=of d] (nd) {\{T.v${}\coloneqq{}$F.v$^{*}$T.v\}};
\node[right=of i] (ni) {\{F.v${}\coloneqq{}$2\}};
\node[left=of b] (nb) {\{F.v${}\coloneqq{}$5\}};
\node[left=of e] (ne) {\{T.v${}\coloneqq{}$F.v\}};
\node[left=of f] (nf) {\{F.v${}\coloneqq{}$8\}};
\foreach \i in {a,b,d,e,f,i}
\draw[dotted] (\i) -- (n\i);
\end{scope}
\end{tikzpicture}

\end{document}


In comments, the annotations were requested to be red and for the tree to have a caption:

\documentclass{scrartcl}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{trees,positioning}

\begin{document}
An example of a cross-reference to Figure~\ref{fig:tree}.

\begin{figure}
\centering
\begin{tikzpicture}
\node (a) {E}
child {node (b) {F}
}
child {node (c) {+}
}
child {node (d) {T}
child {node (e) {T}
child {node (f) {F}  child {node  (g) {8}} }
}
child {node  (h) {*}}
child {node  (i) {F} child {node  (j) {2}}}
};
\begin{scope}[every node/.style={font=\footnotesize,color=red}]
\node[right=of a] (na) {\{E.v${}\coloneqq{}$+T.v\}};
\node[right=of d] (nd) {\{T.v${}\coloneqq{}$F.v$^{*}$T.v\}};
\node[right=of i] (ni) {\{F.v${}\coloneqq{}$2\}};
\node[left=of b] (nb) {\{F.v${}\coloneqq{}$5\}};
\node[left=of e] (ne) {\{T.v${}\coloneqq{}$F.v\}};
\node[left=of f] (nf) {\{F.v${}\coloneqq{}$8\}};
\foreach \i in {a,b,d,e,f,i}
\draw[dotted] (\i) -- (n\i);
\end{scope}
\end{tikzpicture}
\caption{A syntactic tree}
\label{fig:tree}
\end{figure}

\end{document}


• Thank you very much ,how can I change the color attributes ? For example {F.v:=8} //red – GoldRay Apr 30 '13 at 16:56
• @user29570 Colour attributes of what? For nodes you can add [color=<colour>] or create a style for a coloured node or all nodes. If you want to colour the branches, that's a bit more involved. – Alan Munn Apr 30 '13 at 16:59
• I want to change the color of the attributes :{E.v:=+T.v} ,{F.v:=5} ... – GoldRay Apr 30 '13 at 17:18
• @user29570 what exactly do you mean with a title? Do you mean a caption as in "Figure 2: a representation of..."? If this is so, the wrap the entire tikzpicture inside a figure environment and use the standard \caption command. If you don't know how to do that, or if you meant other kind of title, let me know to help you. – Gonzalo Medina Apr 30 '13 at 21:44
• @user29570 No problem. Please see my updated answer. Also, don't forget to accept (different from upvote) the answer that you consider best solved your problem. In case of doubt, please see How do you accept an answer?. – Gonzalo Medina Apr 30 '13 at 22:12