1

I would like to add a label to some of my edges in a tikz-graph.

I have tried the code below, but it adds the label to the parent node - I would like this label to be on the edge instead.

\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,petri}
\usetikzlibrary{positioning}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{calc,shadings}
\usetikzlibrary{graphs}
\usegdlibrary{layered}

\begin{document}
\begin{figure}
\begin{center}
\tikz [baseline=(current bounding box.center)] \graph [layered layout, grow'=right, level sep=1cm, sibling distance=.5cm] {
toto [label=mylabel] ->  {
    a,b
}
};
\end{center}
\end{figure}
\end{document}

Result : Result

Does anyone know how could I get the label on the arrow instead ?

I have also tried this, which did not compile (in lualatex):

\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,petri}
\usetikzlibrary{positioning}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{calc,shadings}
\usetikzlibrary{graphs}
\usegdlibrary{layered}

\begin{document}
\begin{figure}
\begin{center}
\tikz [baseline=(current bounding box.center)] \graph [layered layout, grow'=right, level sep=1cm, sibling distance=.5cm] {
toto ->  {
    a,b
};
\draw (toto) edge node{label} (a);
};
\end{center}
\end{figure}
\end{document}

Thanks in advance for your answers!

4

First of all, it seems that you need a quite bleeding edge Tikz to make this work. But I found the solution on the section 19.2.3 of the tikz-pgf 3.0.0 manual:

add \usetikzlibrary{quotes} in the preamble and then:

\tikz [baseline=(current bounding box.center)] \graph [layered layout, grow'=right, level sep=1cm, sibling distance=.5cm] {
    toto  ->  [red] { % apply to all the edges
        a [> "lab1"], b [> {blue, sloped, pos=0.9, "lab2"'} ]  % apply only to the edge to this node with the ">"
}
};

will produce:

output

...don't ask for the strange value of pos, I found it by trial and error.

  • Thanks ! By tweaking the value of pos and also changing a bit the value of sibling distance to give some breathing space, I've been able to obtain a nice one ;-) – potterman28wxcv May 27 '16 at 11:39
  • 1
    Note the ' that brings the label below the edge :-D – white_gecko Feb 6 at 23:11

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.