Drawing LaTeX trees

Un article de Loria Wiki.

Sommaire

By hand (tree-dvips)

We describe here a frustration-free approach to drawing LaTeX trees by hand. The key to keeping your sanity is to a get a grid representation of your tree on paper before trying to latex it.


Ingredients

  • tree-dvips package
  • paper and pencil

Get a grid representation on paper

  1. Cleanly and clearly draw your tree on paper
  2. Count the number columns in your tree (leaf nodes) as well as the number of nodes
  3. Draw a grid on a piece of paper with the number of rows and columns you just counted
  4. Fill your grid from the bottom-up, left-to-right:
    • If there is no node in that square, indicate it with a small tick or dash
    • If the node has multiple children, draw a box to indicate how many columns it spans

Now proceed to LaTeX

The LaTeX is now child's play.

  • use a tabular environment with as many c's (centre) as columns
  • multicolumn for the spanning nodes
  • use gorn addresses to name the node (i think it's less confusing that way), although if you have big trees, you should probably give separate prefixes to each subtree, to keep your gorn addresses no more than 3 levels deep
  • put all nodeconnect statements for the same parent onto one line

Hint: if you have more than one tree, put each tree into its own tabular. It'll be much easier to manipulate that way.

Deal with errors

pstopdf

See this error? It just means you're using a node name which you have not defined. Check your nodeconnects.

%%[ Error: undefined; OffendingCommand: get ]%%

Stack:
/m1.1.1
-dict-
650.554
395.031

Tweak to perfection

Only do this stage at the END of the process when you have the right tree

  • Trees too wide? Kill some horizontal space between your columns...
    \begin{tabular}{c@{\hspace{-3em}}c@{\hspace{-3em}}c}
  • Node alignments ugly? Play with that \hspace in your multicolumn nodes
    \multicolumn{2}{c}{\hspace{4em}\node{x1.2}{VP$^{idx=c}$}}
Here is an example tree tweaked to taste:
\begin{figure}[htbp]
\begin{center}
\small
% ------------------------------------------ John calls Mary
\begin{tabular}{c@{\hspace{-3em}}c@{\hspace{-3em}}c}
\multicolumn{3}{c}{\hspace{-2em}\node{x1}{S}}
\\[2ex]
\node{x1.1}{NP$^{idx=j}$} &
\multicolumn{2}{c}{\hspace{4em}\node{x1.2}{VP$^{idx=c}$}}
\\[2ex]
\node{x1.1.1}{John} &
\node{x1.2.1}{V} &
\node{x1.2.2}{NP$^{idx=m}$}
\\[2ex]
&
\node{x1.2.1.1}{calls} &
\node{x1.2.2.1}{Mary}
\\
& \textit{\tiny name(j,john), l$_0$:calls(c,j,m), name(m,mary)} & \\
\end{tabular}
\nodeconnect{x1}{x1.1}
\nodeconnect{x1}{x1.2}
\nodeconnect{x1.1}{x1.1.1}
\nodeconnect{x1.2}{x1.2.1}
\nodeconnect{x1.2}{x1.2.2}
\nodeconnect{x1.2.1}{x1.2.1.1}
\nodeconnect{x1.2.2}{x1.2.2.1}
\caption{\label{fig:tag-with-semantics-2} \textsf{John calls Mary}}
\end{center}
\end{figure}

Using a fancy package

  • xy-pic
  • xyling package

Unfortunately, Ralf Vogel himself says that it is not possible to make links between nodes of different trees by using this package. So you're probably best off using tree-dvips if this is something you ever want to do.

Outils personels