1

I would like to have arrows with text (above or below the arrow) connecting the nodes in a forest-environment. Unfortunately, I do not know how to implement it in the code because each arrow should have another text.

I am really grateful for every kind of help. Please find below my code:

\pgfkeys{/forest,
  my rounded corners/.append style={rounded corners=2pt},
}
\begin{forest}
  for tree={
      font=\sffamily,
      line width=1pt,
      draw=linecol,
      drop shadow,
      fit=rectangle,
      edge={thick, color=linecol, >={Triangle[]}, ->},
      where level=0{%
        l sep+=5pt,
        calign=child,
        calign child=2,
        inner color=ocre!70,
        outer color=ocre!70,
        align=center,
        my rounded corners,
        for descendants={%
          calign=first,
        },
      }{%
        where level=1{%
          inner color=ocre!30,
          outer color=ocre!30,
          my rounded corners,
          align=center,
          parent anchor=south west,
          tier=three ways,
          for descendants={%
            child anchor=west,
            parent anchor=west,
            align=left,
            anchor=west,
            inner color=ocre!10,
            outer color=ocre!10,
            edge path={
              \noexpand\path[\forestoption{edge}]
              (!to tier=three ways.parent anchor) |-
              (.child anchor)\forestoption{edge label};
            },
          },
        }{}%
      },
  }
  [a
    [b
      [b1
        [b2]
      ]
    ]
    [c
      [c1
        [c2
          [c3]
        ]
      ]
    ]
    [d
      [d1
        [d2]
      ]
    ]
    [e
      [e1
        [e2]
      ]
    ]
  ]
\end{forest}
Stefan Pinnow
  • 29,535
  • See, e.g., this answer and this question for labeling edges in a forest. Note that for |- and -| paths the position midway or pos=.5 refers to the bend, so if you want the label halfway down the vertical part you'll want to use pos=.25. – Emma Nov 11 '16 at 14:32
  • Welcome! Please make your code compilable. I don't really understand what you want the code to do. Right now, you don't have any edge labels. Do you want the edges labelled? Or do you want arrows with labels separate from the edges themselves? Either way, I think you would find it easier to use the folder style provided by the edges library if I'm right about the general idea of the code you're using. – cfr Nov 11 '16 at 18:00

1 Answers1

4

I'm guessing here on multiple fronts: how to complete your code; what you are trying to do; what the problem is. I suspect you found this code elsewhere. If so, you should attribute it and it would be helpful to have a link. (Hopefully, the original example is complete.)

I've substituted colours as I don't know how yours are defined. I've added libraries and I've tried to simplify things where possible.

I think you want something like this:

possible target image

If so, then I recommend using the folder style from Forest's edges library, which makes life a lot easier when drawing this kind of tree.

Forest has a built-in key for adding labels to the edges called edge label. arrow label is a simple wrapper.

Note that you should not use where within the scope of either for tree or another where as where implicitly uses for tree itself. Use if or take the where outside the scope. Here, I take the first conditional outside the scope of for tree and change the second, which needs to be within the scope of the first, to if.

Saying inner color=<colour>, outer color=<colour> is pointless if <colour> is the same. All it does is slow compilation. fill=<colour> is better in this case.

I've changed the structure of the tree since it seems that it really includes nodes at levels 0, 1 and 2 rather than including nodes at higher levels. Obviously, you can change this back if that's not the case.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shadows,arrows.meta}
\begin{document}

\begin{forest}
  arrow label/.style={
    if level=1{
      edge label={node [midway, font=\scriptsize\sffamily, sloped, above] {#1}},
    }{
      edge label={node [midway, font=\scriptsize\sffamily, anchor=south west] {#1}},
    },
  },
  for tree={
    font=\sffamily,
    line width=1pt,
    draw=darkgray,
    drop shadow,
    edge={thick, color=darkgray, -{Triangle[]}},
    l sep'+=25pt,
  },
  where level=0{%
    fill=blue!70,
    rounded corners=2pt,
  }{%
    if level=1{%
      fill=blue!30,
      rounded corners=2pt,
      child anchor=north,
      for tree={
        grow'=0,
        folder,
        l sep'+=20pt,
      },
      for descendants={%
        fill=blue!10,
      },
    }{}%
  },
  before typesetting nodes={
    for tree={
      content/.wrap value=\strut #1,
    }
  }
  [a
    [b, arrow label=This Way
      [b1, arrow label=An Answer]
      [b2, arrow label=Best Answer]
    ]
    [c, arrow label=Best Way
      [c1, arrow label=A Connection]
      [c2, arrow label=A Way Station]
      [c3, arrow label=A Destination]
    ]
    [d, arrow label=No Way
      [d1, arrow label=By-The-By]
      [d2, arrow label=Side-By-Side]
    ]
    [e, arrow label=Lesser Evil
      [e1, arrow label=Lost In Space]
      [e2, arrow label=Lost In Time]
    ]
  ]
\end{forest}
\end{document}
cfr
  • 198,882