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'm making a tree in a document with tikz-qtree and I managed to make the tree. I've also added labels to the edges, without any problems. However, the labels appear at the top of the edge, i.e. a lot closer to the parent node than the child node. I'd like to have the labels in the middle. How can I accomplish that?

I checked out this question on positioning labels, but i can't figure out from the answer what it actually is they did...

My code is as followed:

\begin{tikzpicture}[every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=1cm,
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree
[.a
    \edge node[auto=right] {$[a]$};
    [.b 
       \edge node[auto=right] {$[b]$};
       [.1 ]
       \edge node[auto=left] {$[b]$};
       [.2 ]
        ]
    \edge node[auto=left] {$[a]$};
    [.c 
        \edge node[auto=right] {$[c]$};
        [.3 ]
        \edge node[auto=left] {$[c]$};
        [.4 ]
        ]
]
\end{tikzpicture}

As provided in the answer, here the actual solution that I used (Note that Christian R.'s solution also works:

\begin{tikzpicture}[every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=1cm,
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree
[.a
    \edge node[auto=right,pos=.6] {$[a]$};
    [.b 
       \edge node[auto=right,pos=.8] {$[b]$};
       [.1 ]
       \edge node[auto=left,pos=.8] {$[b]$};
       [.2 ]
        ]
    \edge node[auto=left,pos=.6] {$[a]$};
    [.c 
        \edge node[auto=right,pos=.8] {$[c]$};
        [.3 ]
        \edge node[auto=left,pos=.8] {$[c]$};
        [.4 ]
        ]
]
\end{tikzpicture}

Giving as result:

enter image description here

share|improve this question
    
Welcome to TeX.SX. –  Claudio Fiandrino Feb 14 '13 at 13:48
add comment

1 Answer

up vote 5 down vote accepted

You could use the option midway, like so:

\edge node[midway,left] {$[b]$};

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=1cm,
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree
[.a
    \edge node[auto=right] {$[a]$};
    [.b 
       \edge node[midway,left] {$[b]$};
       [.1 ]
       \edge node[midway,right] {$[b]$};
       [.2 ]
        ]
    \edge node[auto=left] {$[a]$};
    [.c 
        \edge node[midway,left] {$[c]$};
        [.3 ]
        \edge node[midway,right] {$[c]$};
        [.4 ]
        ]
]
\end{tikzpicture}
\end{document}

Result

image

share|improve this answer
2  
And if you want even finer control, you can use the pos=<value> key where <value> is a fraction of the total length. (I was about to add an answer like yours). –  Alan Munn Feb 14 '13 at 14:25
    
@AlanMunn: Thanks, that totally worked! I was wondering if you could provide me some insight though. To get the labels at approximately the middle I had to use pos=.8 for the second row of labels and pos=.6 for the first. Why isn't it pos=.5? –  Mythio Feb 14 '13 at 14:41
    
@Christian R.: Your solution works too, although it doesn't put the labels very neat. It works well enough though for most cases, and probably the position also depends on the other parameters like sibling spacing etc. –  Mythio Feb 14 '13 at 14:42
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.