3

I'm making a tree using TikZ-qtree, and I need to put labels on my edges. Right now it looks something like this:

\begin{tikzpicture}
\Tree
    [.$\alpha$
        \edge node[left, pos=.2]{\scriptsize $p$};
        [.\phantom{$\beta$} ]
        \edge node[left, pos=.6]{\scriptsize $a$};
        [.$\alpha$ ]
        \edge node[right, pos=.2]{\scriptsize $b$};
        [.$\beta$ ]
]
\end{tikzpicture}

Right now, the "a" is quite far from the center edge and the "b" is quite close to the rightmost edge:

enter image description here

Ideally, the "a" would be closer to the middle edge and the "b" would be slightly farther from the right edge. Does anyone know how to make this happen?

1
  • 2
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – texenthusiast Aug 12 '13 at 18:40
2

One way to achieve better control over the positioning of these nodes is to define them as nodes outside of the \Tree, and then \usetikzlibrary{positioning}. This library makes it easy for you to place nodes relative to one another. Thus, I've defined the topmost part of the tree as a node, and defined the position of the others relative to it:

\documentclass{standalone}

\usepackage{tikz-qtree}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
    \Tree
        [.\node (alpha) {$\alpha$};
            [.\phantom{$\beta$} ]
            [.$\alpha$ ]
            [.$\beta$ ]
        ]

    \node [below left=.1cm of alpha, xshift=.05cm] {\scriptsize $p$};
    \node [below=.2cm of alpha,xshift=-.1cm] {\scriptsize $a$};
    \node [below right=.1cm of alpha, xshift=-.05cm] {\scriptsize $b$};
\end{tikzpicture}

\end{document}

enter image description here

Hopefully, the above code will give you a beginning idea of how you can control the placement of nodes with the positioning library. You may also want to look at section 16.5.3 of the TikZ & PGF manual (p. 185), particularly if you wish to further tweak the placement of these nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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