11

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
7
  • 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. Commented May 16, 2013 at 16:15
  • @morbusg Have you seen here:tex.stackexchange.com/questions/82675/… ?
    – Scott H.
    Commented May 16, 2013 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
    Commented May 16, 2013 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
    Commented May 16, 2013 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. Commented May 17, 2013 at 9:43

2 Answers 2

12

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

7

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.

4
  • 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. Commented Sep 15, 2011 at 16:35
  • 2
    @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
    Commented Sep 15, 2011 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. Commented Sep 16, 2011 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
    Commented Sep 16, 2011 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .