Metapost rotating labels around their center rather than the origin

Instead of

label("\huge$p_2f_1$", (vzero + vtwo) * 0.5);

you can use this:

labeloffset := 7bp; % necessary here to avoid overlapping the arrows
draw thelabel.top("\huge$p_2f_1$", .5[vzero,vtwo]) rotatedaround (.5[vzero,vtwo], angle(vzero-vtwo));

which gives:

enter image description here


If you are doing that lots you might like to make a macro:

vardef label_along(expr your_label, target_path, time_on_path, offset) = 
    draw thelabel(your_label, origin) rotated angle direction time_on_path of target_path
        shifted (up scaled offset rotated angle direction time_on_path of target_path)
        shifted point time_on_path of target_path 
enddef;

Here's a version of your diagram that uses it.

enter image description here

And the source for that:

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
vardef label_along(expr your_label, target_path, time_on_path, offset) = 
    draw thelabel(your_label, origin) rotated angle direction time_on_path of target_path
        shifted (up scaled offset rotated angle direction time_on_path of target_path)
        shifted point time_on_path of target_path 
enddef;

vardef connect(expr a, b) = 
    save arc, barb, alpha;
    path arc, barb; 
    arc = a -- b cutbefore fullcircle scaled 8 shifted a
                  cutafter fullcircle scaled 8 shifted b;
    numeric alpha;
    alpha = angle (b-a);
    barb = (arc -- (right scaled ahlength 
                          rotated (alpha + 180 - 1/2 ahangle) 
                          shifted point 1 of arc))
           shifted (up scaled 1 rotated alpha);
    draw barb; draw barb rotatedabout(1/2[a,b], 180);
enddef;

beginfig(1);

    z0 = -z1 = 42 up;
    z3 = -z2 = 68 right;

    dotlabel.top("$v_0$", z0);
    dotlabel.bot("$v_1$", z1);
    dotlabel.lft("$v_2$", z2);
    dotlabel.rt("$v_3$", z3);

    connect(z0, z1);
    connect(z0, z2);
    connect(z0, z3);
    connect(z1, z2);
    connect(z1, z3);

    label_along("$p_2 f_1$", z2--z0, 0.4, 8);

endfig;
\end{mplibcode}
\end{document}

Compile with lualatex.

  • The key thing about rotations in this sort of work is to rotate the object before you shift it. Or use rotatedabout...

  • ahangle and ahlength are the plain MP parameters used for regular arrow heads.