[Tex/LaTex] Alignment of node labels in tikz-qtree

tikz-qtreetrees

I am trying to draw a refutation tree:

\documentclass{article} 

\usepackage{tikz} 
\usepackage{tikz-qtree} 

\begin{document} 
\begin{tikzpicture} 
     \Tree  
        [.\node[label= {$\langle \texttt{stateA(X)}, 0 \rangle$}] {};    
       [.\node[label= {$\langle \texttt{stateB(X, Y)}, 0 \rangle$}] {};   
            \edge node[left] {$X=a, Y=b$}; [.\node[label={$\langle \texttt{stateC(a,b)}, 0 \rangle$}]  {};   
                 \node[ label=below: {$\langle [], res1 \rangle$}] {};  
                 ]  
            \edge node[right] {$X=a, Y=c$}; [.\node[label={$\langle \texttt{stateD(b, c)}, 0 \rangle$}]  {};  
                 \node[ label=below: {$\langle [], res2 \rangle$}] {};  
                  ]   ]   ] 
\end{tikzpicture} 
\end{document} 

Hovewer, in the resulting tree most labels are not in the right place. How can I possibly fix this?

Initially, I was experimenting with style definitions from this example but circles or rectangles do not seem to be a fit.

Best Answer

A few comments about your tree.

  • It's generally never necessary to use explicit \node commands with tikz-qtree since each [.label ] constituent label is automatically a node.

  • To label an edge of a tree, you should use the [auto=left] and [auto=right] parameters for the node. This will ensure better placement of the label text.

  • When making complicated node labels, your tree will be much more readable if you create some sensible markup for the different components of it.

Here's a version of your tree taking all these ideas into account:

I've created a \brk command to wrap something in angle brackets. I've also created a \state command that puts its argument in \texttt. This way if you decide to change that formatting decision down the line you only need to change one command.

\documentclass{article} 

\usepackage{tikz} 
\usepackage{tikz-qtree} 
\newcommand\state[1]{\texttt{#1}}
\newcommand\brk[1]{$\langle #1 \rangle$}
\begin{document} 

\begin{tikzpicture}[level distance=40pt,sibling distance=25mm] 
  \Tree  
     [.{\brk{ \state{stateA(X)}, 0 }}   
        [.{\brk{ \state{stateB(X, Y)}, 0 }}  
            \edge node[auto=right] {$X=a, Y=b$}; 
            [.{\brk{ \state{stateC(a,b)}, 0 }}   
                 [.{\brk{ [], res1 }} ]  
                 ]  
            \edge node[auto=left] {$X=a, Y=c$}; [.{\brk{ \state{stateD(b, c)}, 0 }}  
                 [.{\brk{ [], res2 }} ]  
                  ]   ]   ] 
\end{tikzpicture} 
\end{document} 

output of code