Savor Your Code.

METAPOST: The Program

Donald Knuth invented the WEB language for literate programming. A literate program reads like a book (or very long article), but can also be compiled into executable code. Literate programming does NOT mean fancy API documentation; it means the programmer can present source code in the order most suitable for exposition. WEB allows the programmer to express how to extract a TeX document and valid source files from the same document.

In any case, METAPOST is written in WEB, meaning both a TeX document and an executable can be created from the same source. This article describes how to get a TeX document from the METAPOST sources.

A WEB program is turned into a TeX document using the WEAVE program; similarly, the TANGLE program will produce source code that can be compiled. There isn't a single WEB language for all types of source code; rather, specific WEBs exist for each source language. METAPOST is written in CWEB, the WEB for the C language. Therefore, the WEAVE and TANGLE programs for CWEB are CWEAVE and CTANGLE.

The METAPOST project is hosted at The sources are in SVN and can be downloaded following directions found on that site. The METAPOST source itself is located a ways down the directory tree: /trunk/metapost/source/texk/web2c/mplibdir. In mplibdir you will find 9 ".w" files, each representing a portion of METAPOST: mp.w, mpmath.w, mpmathdouble.w, mpost.w, mpstrings.w, mpxout.w, psout.w, svgout.w, and tfmin.w.

To create TeX documents from these files, simply execute CWEAVE on them. If you have a modern TeX distribution installed (such as TeXLive 2011), then CWEAVE should already exist on your system:

  cweave mp.w
  cweave mpmath.w

Each invocation will produce a corresponding ".tex" file (e.g., "mp.tex", "mpmath.tex", etc.). Producing a PDF is easy as:

  pdftex mp
  pdftex mpmath

Some TeX errors seem to have crept into the source (when was the last time someone tried this, I wonder?), but even so you still get a book size document describing the implementation of METAPOST. Note that each document appears standalone. The largest, by far, is "mp.pdf."

Category: None

Please login to comment.


  1. Cool! by Erin (2012-06-12)

    That is neat! Thanks for sharing.