# OpenType fonts for Metapost Labels

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
-
Thanks to a patch by Dohyun Kim there are now btex ... etex commands available in luamplib. I’m not sure if it’s in the pretest already but it’s worth a try. – Philipp Gesang May 16 '13 at 16:15
@morbusg Have you seen here:tex.stackexchange.com/questions/82675/… ? – Scott H. May 16 '13 at 17:38
@ScottH. I had, but I had forgotten! Well there goes rep, but I didn't want to ask the exact same question. – morbusg May 16 '13 at 21:10
@phg: Thanks for that info! If someone wants to put that into an answer I'll throw some bounty on his/her way (maybe with some patch instructions or something). Doesn't exactly bring closure to the OP, but hey. – morbusg May 16 '13 at 21:18
@morbusg: The credit for the patch goes to Kim alone, we merely ported it. If it works for you, just send him a thank you email. – Philipp Gesang May 17 '13 at 9:43

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:

-

If you don't mind using ConTeXt to generate your metapost figures you can just use:

% ConTeXt font setup. This is similar to fontspec
% \usemodule[simplefonts]
% \setmainfont[Pagella]

\startMPdefinitions
% Common MP code for all figures
\stopMPdefinitions

\starttext
\startMPpage
% MP figure. Text enclosed in btex ... etex is parsed using ConTeXt
\stopMPpage
...
\startMPpage
...
\stopMPpage
\stoptext

And then compile it using context filename.

PS: I am not sure what is the current status of ConTeXt on MikTeX. If MikTeX does not include ConTeXt, you could just install ConTeXt standalone (in a parallel tree) which is about 250MB.

-
Pardon my ConTeXt ignorance, but can I generate PS or SVG output from ConTeXt? I want to include the output in my existing LaTeX documents. – Colin Fraizer Sep 15 '11 at 16:35
@Colin: By default ConTeXt MkIV (the one that uses luatex engine) produces pdf. If you are using pdflatex you can use directly use the pdf. For latex, you will need to convert the pdf to eps (there are plenty of tools for that). ConTeXt does not generate SVG output. – Aditya Sep 15 '11 at 18:49
I'm sorry to be such a bozo, but this still doesn't work for me. My labels are all just an empty square, even if I use the default fonts. – Colin Fraizer Sep 16 '11 at 13:55
@Colin: A minimal example works fine here, so I am afraid that more details are needed on why things fail on your machine. This format of this forum is not suited to debugging such issue. Would you mind posting a minimal failing example along with the version of ConTeXt and Metapost that you are using on the ConTeXt mailing list (or even the metapost mailing list) – Aditya Sep 16 '11 at 19:19