2

When I add labels to MetaPost drawings in ConTeXt, it does not use the fonts for the rest of the document but appears to be using either a math font or the default fonts used by ConTeXt. I can set the font for specific items, e.g. label("\sourcefont This will appear with the correct font", (25,75)); but doesn't seem to follow the philosophy of ConTeXt, and I have to set the font for every single label (I have many hundreds). Is there a global way to set it, either for within all MetaPost drawings in the document, or in the very least, at the start of the MetaPost, which will be used everywhere a label is placed?

5

The default font for Metafun labels is set in meta-ini.mkiv to MetafunDefault which is a synonym for Regular which is usually the serif text font, by default Latin Modern Roman.

Thus you have two options to change the font.

  1. Reset the MetafunDefault synonym to a font of your choice:

    \definefontsynonym [MetafunDefault] [file:texgyrepagella-regular*default]
    \starttext
    This is the text font
    
    \startMPcode
    label("This is the Metafun font", origin) ;
    \stopMPcode
    \stoptext
    
  2. Use MPinitializations do set the defaultfont variable to something different. This is the documented approach, see 10.3 “Labels” in the Metafun manual.

    \startMPinitializations
    defaultfont := "file:texgyrepagella-regular*default" ;
    \stopMPinitializations
    \starttext
    This is the text font
    
    \startMPcode
    label("This is the Metafun font", origin) ;
    \stopMPcode
    \stoptext
    
  • I see in one of my files (probably from the list) \startMPenvironment \setupbodyfont[9pt]\stopMPenvironment. Is that a worse solution than the ones you suggest for some reason? – mickep May 6 at 14:56
  • @mickep From reading the Metafun manual it is not clear to me whether this is equivalent or orthogonal to my answer. You should definitely post it as an additional answer. – Henri Menke May 6 at 23:23
2

I use the textstyle key of MPinstance for changing the font. The default metapost figures are drawn using the metafun instance. So, you can simply do:

\setupMPinstance[metafun][textstyle={\sourcefont}]

Here is a complete example:

\setupbodyfont[modern,10pt]

\setupMPinstance[metafun][textstyle={\switchtobodyfont[pagella,10pt]}]

\starttext
This is the text font

\startMPcode
label("This is the Metafun font", origin) ;
\stopMPcode
\stoptext

which gives

enter image description here

1

I hesitated to give this as an answer, since I read in the MetaFun manual that the \startMPenvironemnt \stopMPenvironment is merely for mkii, and not needed in mkiv (see section 10.2).

In any case, I have successfully used this method for years:

\setupbodyfont[bonum]

\startbuffer[lefigure]
\startplacefigure[location=nonumber]
\startMPcode
draw fullcircle scaled 2cm;
label.rt(textext("east"),(1cm,0));
label.top(textext("north"),(0,1cm));
label.lft(textext("west"),(-1cm,0));
label.bot(textext("south"),(0,-1cm));
\stopMPcode
\stopplacefigure
\stopbuffer

\startTEXpage[offset=0.25in]
Normal text set with bonum. MetaPost figures uses the same font:

\getbuffer[lefigure]

\startMPenvironment
\setupbodyfont[latinmodern,ss,6pt]
\stopMPenvironment

Efter we have changed the bodyfont inside a \tex{startMPenvironment} \tex{stopMPenvironment}
we get this new font in our MetaPost figures:

\getbuffer[lefigure]
\stopTEXpage

image showing result with different fonts in different parts

I would not suggest to do such changes more than once, but once and for all. Typically:

\setupbodyfont[bonum]
\startMPenvironment
\setupbodyfont[6pt]
\stopMPenvironment

\starttext
...
\stoptext

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.