Labels in Tikz Forest – Positioning Tips

forestlabelstikz-trees

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}

Best Answer

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

Related Question