MetaPost Examples

2008-10-19: I use PyX instead of Metapost. It’s based on Python so the syntax is clear and straightforward, and is easily used from Sage (install with “sage -python install” from the PyX source directory).


My favourite package for drawing diagrams is MetaPost. Here’s an example, taken from my PhD thesis:

Metapost diagram

The best page for examples is the one by (La)TeX Navigator.

Compiling with LaTeX fonts

Normally MetaPost uses TeX to compile but it is nicer to have access to LaTeX fonts/symbols/etc. To do this put the following at the top of your MetaPost file:




Then set the TEX environment variable and compile. For example, in
Bash, you could do:

$ export TEX=latex && mpost

My examples

Here are all the MetaPost diagrams from my PhD thesis. There are a few duplicates and some dodgy code, I make no claim of being an expert in MetaPost.


Browse files

All files in a compressed
tar archive

Metapost to PDF

A few of my Metapost files use the following TEX function (copied from here):

vardef TEX primary s =
   write "verbatimtex"                    to "";
   write "documentclass[12pt]{article}"  to "";
   write "usepackage[T1]{fontenc}"       to "";
   write "usepackage{amsmath,amssymb}"   to "";
   write "begin{document}"               to "";
   write "etex"                           to "";
   write "btex "&s&" etex"                to "";
   write EOF                              to "";
   scantokens "input mptextmp"

The command mptopdf seems to have trouble with the temporary file In particular I got this error when doing mptopdf

This is MetaPost, Version 0.901 (Web2C 7.5.5)
( (
>> mptextmp.mpx
! Unable to make mpx file.
l.5 btex
         1 etex
Transcript written on bug.log.
 error in metapost run :

        total run time : 0 seconds

MPtoPDF 1.3 : error while processing mp file

On the other hand, this seems to work:

mptopdf bug.1

I’m not sure why this is the case – is this a bug or just undocumented behaviour?

Other stuff

MetaFun looks good. It’s also worth checking out what other peope have tagged as metapost on

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s