# How can I get Graph edge labels to parallel an edge line or or follow an edge curve?

Edge labels are always rendered horizontally. I would like the label for any straight line edge to be rotated so the bottom of the label's bounding box is coincident with the edge.

There was an older post (Comfortable Edge Labeling of Undirected Graph) that did this using EdgeRenderingFunction, but that has been superseded by EdgeShapeFunction in v12. EdgeShapeFunction does not have access to the label like EdgeRenderingFunction did.

Labels following a curved edge would be a bonus, but just paralleling a tangent somewhere on the edge would suffice.

• Take a look at this. Commented Feb 13, 2020 at 2:30

You can get the old GraphPlot function in version 12.0.0 using GraphComputationGraphPlotLegacy.

Using a modification of the function erf from this answer:

ClearAll[erf];
erf[pos_List: {.5}, lblcolors_: {Black}, fontsizes_: {12},
linecolor_: Directive[Opacity[0.7], Hue[0.6, 0.7, 0.5]]] :=
{linecolor, Line[#],
Sequence @@ Table[Inset[Style[Text[#3], lblcolors[[i]],
FontSize -> fontsizes[[i]]],
pos[[i]] First@# + (1 - pos[[i]]) Last@#, {Center, Bottom},
Automatic, -Subtract @@ Sort[#], Background -> None],
{i, 1, Length@pos}]} &


Examples:

edges = {{4 -> 2}, {5 -> 1}, {3 -> 2}, {2 -> 5}, {3 -> 5}};
labelededges = MapIndexed[{First@#1, ToString@First@#2} &, edges];

GraphComputationGraphPlotLegacy[labelededges,
EdgeRenderingFunction -> erf[],
PlotStyle -> Thick, VertexLabeling -> True,
ImagePadding -> 20, ImageSize -> 500]


SeedRandom[1]
labelededges2 = Transpose[{Flatten@edges, StringTake[ RandomWord[5], 4]}];

GraphComputationGraphPlotLegacy[labelededges2,
VertexLabeling -> True,
EdgeRenderingFunction -> erf[], ImageSize -> 500]
`

• I've marked this as a solution, but I actually prefer the answer you gave in the link posted by @Rohit in the comment attached to my original question for three reasons: 1) it uses current, not deprecated, functions, 2) it shows the rendering along a curve, 3) it adds the bonus of multiple labels. Commented Feb 13, 2020 at 18:11