# How to label two edges differently in compact tree graph syntax

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}


And here is the picture:

Below is a possible solution:

\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$"] {(v2) -> {(v4) -> {(l1),(l2)},
(v5) -> {(l3),(l4)}}},
(v1) -> ["$y$"'] {(v3) -> {(l5),(l6)}}
};
\end{tikzpicture}
\end{document}

• Thanks citsahcots! I had thought about this type of solution and sadly it ruins the compactness of the syntax in precisely the way I feared it would (see the second paragraph in my question). This is highly unhandy for my purposes :( is there a way of labelling without splitting the graph syntax? Aug 18, 2021 at 10:11
• @catlpz, you are welcome. Sorry that I do not know how to do that.. Aug 18, 2021 at 10:15

This can be done by:

1. shifting [tree layout, ...] to options passed to the \graph command, rather than passing them as options to the tikzpicture environment.
2. applying the required edge labels with the > syntax. Use ' to shift the label below the edge. An edge label such as x could also be written as >"$x$",>pos=0.5,>inner sep=0pt to apply the label, positioning and separation from the edge to this particular edge label.

\documentclass[tikz,border=3pt]{standalone}

\usetikzlibrary{graphs,graphdrawing,quotes}
\usegdlibrary{trees}

\tikzset{%
every node/.style={draw, circle, inner sep=0pt},
node_a/.style={minimum size=1.5mm},
every edge quotes/.append style={draw=none,inner sep=0pt}
}

\begin{document}
\begin{tikzpicture}
\graph[tree layout,
grow'=right,
sibling distance=5mm]{%
v1 [as={$v_1$}] -> v2 [as={$v_2$},>"$x$"]; % use > to apply the label to the edge
v1 -> v3 [as={$v_3$},>"$y$"']; % use > to apply the label to the edge and ' to shift below
v2 -> v4 [as={$v_4$}];
v2 -> v5 [as={$v_5$}];
v3 -> v6 [as={},node_a];
v3 -> v7 [as={},node_a];
v4 -> v8 [as={},node_a];
v4 -> v9 [as={},node_a];
};
\end{tikzpicture}
\end{document}