12

I'm constructing a tree diagram using the forest package, and I want to label the edges. Usually, Tikz is quite good at avoiding a collision between the label and the edge, but if the branches of my tree are forced to spread out too much then the label starts to overlap the edge.

Is there a way of manually offsetting the labels I've added? Or perhaps there is a way of getting the labels to interrupt the edge, much like the description option for labels in tikzcd?

Tree diagram with bad edge label positioning

\documentclass[a4paper,10pt]{article}

\usepackage{tikz}
\usepackage{forest}

\begin{document}
\begin{forest}
  for tree={grow'=north}
  [A
    [B, edge label={node[midway,left,font=\scriptsize]{1}} [] [] [] [] [] [] [] [] [] []]
    [C, edge label={node[midway,left,font=\scriptsize]{2}} [] [] [] [] [] [] [] [] [] []]
  ]
\end{forest}

\end{document}
  • A tip: try yshift= and some dimension in the nodes. – Malipivo Apr 2 '15 at 15:45
18

You can use auto instead of `left:

enter image description here

Or use above:

enter image description here

Or below

enter image description here

There is also above left above right below right above=2mm below=1cm etc. Choose whatever you like.

\documentclass[a4paper,10pt]{article}

\usepackage{tikz}
\usepackage{forest}

\begin{document}
\begin{forest}
  for tree={grow'=north}
  [A
    [B, edge label={node[midway,below,font=\scriptsize]{1}} [] [] [] [] [] [] [] [] [] []]
    [C, edge label={node[midway,below,font=\scriptsize]{2}} [] [] [] [] [] [] [] [] [] []]
  ]
\end{forest}

\end{document}

If you want the labels to interrupt the edges, remove left or whatever you are using and use fill=white

\documentclass[a4paper,10pt]{article}

\usepackage{tikz}
\usepackage{forest}

\begin{document}
\begin{forest}
  for tree={grow'=north}
  [A
    [B, edge label={node[midway,fill=white,font=\scriptsize]{1}} [] [] [] [] [] [] [] [] [] []]
    [C, edge label={node[midway,fill=white,font=\scriptsize]{2}} [] [] [] [] [] [] [] [] [] []]
  ]
\end{forest}

\end{document}

enter image description here

  • 1
    \edge label={node[xshift=5mm, yshift=8mm]{TEXT}} for random position – Fibo Kowalsky Feb 29 '16 at 2:44
  • in case of right below above left the node spins around a point that it at the top of the branch. In case of midway it is in the middle of the branch. What about putting the label in the bottom of the branch? e.g., near A. – Fibo Kowalsky Feb 29 '16 at 2:55
  • @FiboKowalsky: Actually A itself is a node. So you can label it and use it as an anchor to put a new node below it. – user11232 Mar 27 '16 at 3:14
7

This is an addendum to Harish Kumar's answer.

If you want the same positioning for the edge labels for the whole tree, you can put the specification for the creation of the labels in for tree at the start, and then just write edge label=1, edge label=2 etc. in the tree itself. For example, suppose that you wanted the labels above the edges. Then you could use the following:

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

\usepackage{forest}

\begin{document}
\begin{forest}
  for tree={
    grow'=north,
    delay={
      edge label/.wrap value={node[midway, font=\sffamily\scriptsize, above]{#1}},
    },
    font=\sffamily,
  }
  [A
  [B, edge label=1 [] [] [] [] [] [] [] [] [] []]
    [C, edge label=2 [] [] [] [] [] [] [] [] [] []]
  ]
\end{forest}

\end{document}

edge labels with less typing

If you want the flexibility of specifying different positions, you can set up a style, my edge label, which takes two arguments: the first is the required position and the second the label content. For example:

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

\usepackage{forest}

\begin{document}
\forestset{
  my edge label/.style 2 args={
    edge label={node[midway, font=\sffamily\scriptsize, #1]{#2}},
  },
}
\begin{forest}
  for tree={
    grow'=north,
    font=\sffamily,
  }
  [A
  [B, my edge label={below}{1} [] [] [] [] [] [] [] [] [] []]
    [C, my edge label={above}{2} [] [] [] [] [] [] [] [] [] []]
  ]
\end{forest}

\end{document}

flexible edge labels with less typing

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.