I am trying to draw a tree in TikZ as pictured below. Everything is as I want it to be except that I cannot figure out how to ask LaTeX to draw two different labels on the edges in the tree/graph syntax below: I'd like to have the upper edge emanating from the root vertex v1
and ending at v2
labelled as $x$
, the lower ending at v3
as $y$
. However, placing the labels in the way I put them in the code below gives me both edges labelled by both $x$
and $y$
. I tried placing the labels elsewhere (just in front of the child nodes v2
and v3
, e.g.) but that throws an error: LaTeX doesn't seem to know what [" "]
means in front of a node and needs the labelling command do sit right after the arrow ->
. I also tried to add the labelling after specifying the graph but then LaTeX does not remember what the vi
refer to.
I read a lot of the pgf documentation but didn't find any clues as to how to combine the libraries quotes and graphs, graphdrawing and the tree layout correctly.
I guess I could split up my code to specify all parent/child relations (v1) -> (v2), (v1) -> (v3), ...
separately rather than using the nested form (v1) -> {(v2),(v3)}
and then label the edges individually. But that would completely ruin the compactness of the syntax -- not good, as this is only a small MWE and eventually I want to draw a much bigger tree with all edges labelled in some way.
Question 1: is there an easy way to do this? How/where can I put my labels in the code as is?
Question 2: for my bigger purposes much better even, can I add a few lines of code before specifying the graph structure, saying something like for all children j of node i, label the edge (i,j) by $x_ij$
? If so, how?
Here is the MWE:
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing,quotes}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[tree layout,
grow'=right,
sibling distance=5mm]
\foreach \i in {1,...,5}
\node (v\i) [draw, circle, inner sep=0pt] {$v_{\i}$};
\foreach \j in {1,...,6}
\node (l\j) [draw, circle, inner sep=2pt] {};
\graph{
(v1) -> ["$x$","$y$"'] {(v2) -> {(v4) -> {(l1),(l2)},
(v5) -> {(l3),(l4)}},
(v3) -> {(l5),(l6)}}
};
\end{tikzpicture}
\end{document}