I use Metapost to create pdf figures to include in the book I am writing. I usually create pdf files with the command TEX=latex mpost figure.mp and then I convert the output files figure.1, figure.2 to pdf. I am not interested in integrating Metapost directly in my main files, I am confortable with managing separate pdf files and include them \includegraphics{figure-1.pdf} etc.

However, I would like to use in the figures labels the same (.ttf) fonts I use for my text. Unfortunately, I have no idea how to instruct Metapost to use the xetex engine to create labels, provided that this is possible.

  • yes, use luamplib and lualatex and standalone.cls
    – Thruston
    Commented Oct 10, 2022 at 8:37

1 Answer 1


Here is a suggested workflow that you might like to adopt. It involves using lualatex with the luamplib package to create PDF files directly from your Metapost source.

  1. I am assuming you have an MP source file called figure.mp that contains two beginfig()/endfig pairs that produces figure-1.mps and figure-2.mps which you convert to PDF.

  2. Split this file up into two separate files using this template.

     \setmainfont{<whatever TTF font you are using with XeLateX>}
       <your MP code here>
  3. Name the files figure-1.mp and figure-2.mp. (Strictly speaking, the files are LaTeX source files, so you might want to use .tex extension, but I prefer to use .mp because that makes my editor load the correct syntax colouring).

  4. Compile them with lualatex figure-1.mp and lualatex figure-2.mp to create figure-1.pdf and figure-2.pdf

  5. Include them in your book with \includegraphics{figure-1} and \includegraphics{figure-2}

lualatex supports fontspec in exactly the same way as xelatex.

I've written a lot more about this workflow and working with luamplib in my notes on Drawing with Metapost which you might like to read. In particular, you might find section 3.2 and section 12 useful.


  • Syntax highlighting -- using this idea means that the source files are Metapost, wrapped up in LaTeX. This will confuse most syntax highlight engines in most editors. It is possible in Vim highlighting to define different rules for different regions of a file, so you could define the mplibcode environment as a Metapost region in a LaTeX file, but I have found it tricky to do this reliably. My solution is just to force MP syntax highlight for the whole file by using an .mp suffix and setting up the appropriate file type trigger. In Vim you can also force this by doing set ft mp explicitly.

  • Common LaTeX code -- If you have a common set of LaTeX code to share among all your MP source files, then you should probably put all the common elements into a shared file and then include this in each MP source file. For my "Drawing with Metapost" project, I put some common colour and listings definitions into a style file called dwmpcode.sty, and then I have \usepackage{dwmpcode} at the top of my main document and at the top of any MP drawings that need to use the common definitions

  • Common Metapost code -- If you have a common set of Metapost code to share among all your MP source files, then it's a bit simpler. Put all the common code into a Metapost source file, perhaps "my-common-code.mp" and then add input my-common-code as the first line of the mplibcode environment in each source file.

  • Great, thank you! I only have a problem with this solution: often my figures are related and share some variable or code. How can I do if I separate figures into separate .tex files? A second (possibly minor) problem is that by embedding the code into a latex file I cannot benefit from metapost syntax highlighting. Working in a single .mp file is much more comfortable, but I understand that we cannot have everything, can we?
    – Massimo
    Commented Oct 10, 2022 at 17:49
  • This was very useful. In meanwhile, I adapted the workflow for use within org-mode (in Emacs), so that each figure is compiled through org-babel (it required a bit of hacking and programming, but not so difficult). This solution also allowed me to have syntax highlighting and common code. Thanks agains, this will be a major improvement in my use of Metapost
    – Massimo
    Commented Oct 13, 2022 at 5:48

You must log in to answer this question.

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