[Tex/LaTex] OpenType fonts for Metapost Labels

luatexmetapostopentype

Is it possible to use OpenType fonts in MetaPost labels?

I use MiKTeX 2.9. I hoped something like mpost --tex=dvilualatex <myfile>.mp might work, but no luck.

Suggestions?

I tried this, but I get an empty box where my label should be, even if I use the default fonts.

\startMPdefinitions
\stopMPdefinitions


\starttext
\startMPpage
beginfig(1);
  draw(0, 0)--(200,200);
  dotlabel.bot("ABCDEFG", (100,100));
endfig;
\stopMPpage

\stoptext

Best Answer

Aside from ConTeXt, it is also possible to include OpenType fonts in MetaPost labels if you use MetaPost through Lua(La)TeX.

As it was noted in the comments, thanks to Dohyun Kim the btex … etex flags are now recognized by luamplib, but in the recents versions it goes much further than that: luamplib now also includes a textext function that is pretty much the same as its namesake in MetaFun/ConTeXt.

Like the btex … etex flags, textext() (which takes a string of characters as argument) uses by default the fonts of the current Lua(La)TeX document, OpenType or not, to typeset the labels. But it has two big advantages on the btex … etex flags:

  • it allows the treatment of variables in labels; see Metapost label based on variable value;
  • if you issue the \mplibtextextlabel{enable} instruction in the preamble, it is enough to give the string itself as argument to the label commands; textext is called to typeset it, but in the background. Very handy if you have a great bunch of labels to typeset!

So the following program:

\documentclass[12pt]{scrartcl}
\usepackage{unicode-math}
    \setmainfont{XITS}
    \setmathfont{XITS Math}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
    label(textext("Pythagorean addition: $a^2+b^2 = c^2$."), origin);
endfig;
\end{mplibcode}
\end{document}

gives exactly the same result as this:

\documentclass[12pt]{scrartcl}
\usepackage{unicode-math}
    \setmainfont{XITS}
    \setmathfont{XITS Math}
\usepackage{luamplib}
    \mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
beginfig(1);
    label("Pythagorean addition: $a^2+b^2 = c^2$.", origin);
endfig;
\end{mplibcode}
\end{document}

Either way you get 12pt-sized OpenType text and math fonts from XITS (which are themselves adaptation of the STIX fonts) in the label:

enter image description here

Related Question