2

Is there any way to draw and/or fill any path around labels in MetaPost/MetaFun?

According to the manual (page 27) we can use fill/unfill with bbox and thelabel, however there is nothing about, let's say, label@#. I obviously tried the manual examples with label@# but it seems to fail.

Plus, I failed to correctly set a custom path (an ellipse, a star, a triangle, whatever) with setbound.. to.. around the label to draw/fill.

fill unitsquare scaled 1cm xscaled 3 yscaled 3 withcolor gray ;
picture l ; l = thelabel(btex $A$ etex, (1cm,1cm));
unfill bbox l ; draw l ;

gives us

enter image description here

So I tried to set a "custom" circle around the label instead of the default llcorner p--lrcorner p--urcorner p--ulcorner p--cycle:

fill unitsquare scaled 1cm xscaled 3 yscaled 3 withcolor gray ;
picture l ; l = thelabel(btex $A$ etex, (1cm,1cm));
path b ; b = llcorner l..lrcorner l..urcorner l..ulcorner l..cycle ;
setbounds l to b ;
fill bbox l withcolor green ;
draw l ;

which does not return the expected result:

enter image description here

What is the correct way to do all of that?

2 Answers 2

1

You can do what you want also with plain Metapost. You were very nearly there but you just needed to make it a bit simpler:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
fill unitsquare scaled 1cm xscaled 3 yscaled 3 withcolor 1/2;
picture l; l = thelabel(btex $A$ etex, (1cm,1cm));
path b; b = llcorner l..lrcorner l..urcorner l..ulcorner l..cycle ;
fill b withcolor green ;
draw l;
endfig;
\end{mplibcode}
\end{document}

compile this with lualatex (or adapt for plain MP) to get this:

enter image description here

Your b path is circular as you expected, but bbox b is a square. So all I have done differently is to fill b rather than bbox b. The bbox macro always produces a square. It also adds bboxmargin space around the path or image. So you could have written

path b; b = for i=1 upto 4: point i of bbox l .. endfor cycle;

which would make a less tight circle, like this:

enter image description here

But in general it is probably easier to draw the shape you want first, then add a label to the center of it. So you could have done:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
    fill unitsquare scaled 1cm xscaled 3 yscaled 3 withcolor 1/2;
    path b; b = fullcircle scaled 16 shifted (1cm, 1cm);
    fill b withcolor green;
    label(btex $B$ etex, center b);
endfig;
\end{mplibcode}
\end{document}

which is a bit "cleaner" perhaps? (Although you might have to guess at the diameter of the label with a bit of trial and error.)

enter image description here

0

You are not so far away, but the bbox is always a rectangle. Here is an example, where I used curved to convert the bouding box. I also added one option with some randomness.

The wonderful MetaFun manual is full of nice things to pick up. Appendix C is very convenient when it comes to building and transforming paths. Have (Meta)Fun!

\startMPpage[offset=1dk]
fill unitsquare scaled 1cm xscaled 3 yscaled 3 withcolor "darkgray" ;
picture l ; l := thelabel(btex $A$ etex, (1cm,1cm)) ;
path b ; b := curved boundingbox l  ;
fill b withcolor darkgreen ;
draw l ;
draw boundingbox l withcolor "orange" withpen pencircle scaled 0.1;

l := thelabel(btex $B$ etex, (2cm,2cm)) ;
b := (curved boundingbox l) 
     randomrotatedcontrols 1cm ;
fill b withcolor darkred ;
draw l ;
draw boundingbox l withcolor "orange" withpen pencircle scaled 0.1;
\stopMPpage

circled A and B

You must log in to answer this question.