[Tex/LaTex] How to lengthen edge in forest to be the length of the edge label

foresttikz-pgf

How can I increase the length of the edges in a forest tree so that they are (at least) the length of the edge label that I wish to give them.

For example, this code:

\documentclass{article}
\usepackage{forest}
\forestset{ew edges/.style={for tree={parent anchor = east, child anchor = west, grow' = east}}}
\begin{document}
\begin{forest} ew edges
    [96
        [48, edge label = {node [midway, above, sloped] {matched place} }]
        [48, edge label = {node [midway, below, sloped] {unmatched place} }]
    ]
\end{forest}
\end{document}

produces the following tree

enter image description here

How can I make the edges (at least) the length of "unmatched place" so that there isn't such overlap?

Best Answer

The packing system does not take account of edge labels, as explained on page 33. So the simplest way to do this is to adjust the minimum distance between levels (l sep) and/or the minimum distance between siblings (s sep) by hand.

For example, adding s sep+=50pt and l sep+=50pt to an expanded style, ew edges new, produces the following tree:

adjusted tree

Alternatively, it is possible to override the packing mechanism's decisions. To do this, you need to make adjustments after the packing stage but before the final positions are calculated. This can be done, for example, using before computing xy. Using a new style my edge label to adjust the distance between levels, for example, you can obtain a tree such as this:

auto-adjusted levels

This does require you to specify the edge label in a different way. Rather than specifying it using edge label directly, you need to use my edge label. (Obviously, you can use a different name - but you can't use edge label.) This is necessary because the new style manipulates the label to measure its width. This is then used to calculate an appropriate distance between the levels using Pythagoras' theorem.

Note that this second method cannot deal with labels which are too long because the dimensions involved in the calculation become too great. If you need to accommodate really long labels, you should probably adjust the calculation to minimise the sizes of the dimensions involved.

\documentclass[border=5pt, tikz, multi, varwidth]{standalone}
\usepackage{forest}
\standaloneenv{forest}
\begin{document}
\forestset{
  ew edges new/.style={
    for tree={
      parent anchor = east,
      child anchor = west,
      grow' = east,
      s sep+=50pt,
      l sep+=50pt
    }
  },
  ew edges/.style={
    for tree={
      parent anchor = east,
      child anchor = west,
      grow' = east,
    }
  },
  my edge label/.style={
    if={n>.5*(n_children("!u"))}{
      edge label = {node [midway, below, sloped] {#1}},
    }{
      edge label = {node [midway, above, sloped] {#1}},
    },
    TeX={\settowidth{\mylabelwidth}{#1}},
    before computing xy={
      if={
        (l)<(sqrt((\mylabelwidth)^2 - s^2) + 10pt)
      }{
        l={sqrt((\mylabelwidth)^2 - s^2) + 10pt},
      }{},
    },
  },
}
\newlength{\mylabelwidth}
\begin{forest}
  ew edges new
    [96
      [48, edge label = {node [midway, above, sloped] {matched place} }
        ]
        [48, edge label = {node [midway, below, sloped] {unmatched place} }
        ]
    ]
\end{forest}
\begin{forest}
  ew edges
    [96
        [48, my edge label = matched place
        ]
        [48, my edge label = unmatched place
        ]
    ]
\end{forest}
\end{document}
Related Question