4

Currently I am trying to to rotate some labels so that they match the orientation of lines in a diagram.

For example like this: enter image description here

The problem with that example is that the label was rotated around the origin, rather than it's own axis, which moves it from the position it should be. This is the same diagram without rotating the label:

enter image description here

As you can see, the position of the label is actually in the middle of the segment. Normally to rotate the way I want you first rotate and then translate, but I haven;t been able to apply the rotation to the label before moving it.

This is my code:

\documentclass[border=6cm]{standalone}

\usepackage{luamplib}
\mplibnumbersystem{double}
\mplibtextextlabel{enable}
\usepackage[margin=0.5cm]{geometry}

\begin{document}
{\centering
\begin{mplibcode}
u:=2cm;

vardef line(expr pfirst, psecond)=
    m := 0.07;
    pair tangent, normal, off, ofs, osf, oss, pointOne, pointTwo;
    tangent := psecond - pfirst;
    tx := xpart tangent;
    ty := ypart tangent;
    numeric len;
    len = sqrt(tx*tx + ty*ty);
    tangent := tangent * 1/len;
    normal := tangent rotated 90;

    pointOne := pfirst + tangent * 0.2;
    pointTwo := psecond - tangent * 0.2;

    off := pointOne - normal * m;
    ofs := pointTwo - normal * m;
    draw u*off--u*ofs withpen pencircle scaled 2 withcolor black;
    draw u*off--u*(off + (normal * 0.15 rotated 240)) withpen pencircle scaled 2 withcolor black;   

    osf := pointOne + normal * m;
    oss := pointTwo + normal * m;
    draw u*(osf)--u*(oss) withpen pencircle scaled 2 withcolor black;   
    draw u*oss--u*(oss + normal * 0.15 rotated 60) withpen pencircle scaled 2 withcolor black;  
enddef;

input latexmp;
% Start figure
beginfig(0);


pair vzero, vone, vtwo, vthree;  
vzero := (0, 1);
vone := (0, -1);
vtwo := (-2, 0);
vthree := (2, 0);

line(vzero, vone);
line(vzero, vtwo);
line(vtwo, vone);

line(vone, vthree);
line(vthree, vzero);
line(vone, vthree);


vzero := u*vzero;
vone := u*vone;
vtwo := u*vtwo;
vthree := u*vthree;
fill fullcircle scaled 8bp shifted vzero;
fill fullcircle scaled 8bp shifted vone;
fill fullcircle scaled 8bp shifted vtwo;
fill fullcircle scaled 8bp shifted vthree;

label.top("\huge$v_0$", vzero + (0, 0.2) * u);
label.bot("\huge$v_1$", vone + (0,-0.2) * u);
label("\huge$v_2$", vtwo + (-0.4, 0) * u);
label("\huge$v_3$", vthree + (0.4, 0) * u);

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

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

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

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.