MetaPost


MetaPost refers to both a programming language and the interpreter of the MetaPost programming language. Both are derived from Donald Knuth's Metafont language and interpreter. MetaPost produces vector graphic diagrams from a geometric/algebraic description. The language shares Metafont's declarative syntax for manipulating lines, curves, points and geometric transformations. However,
Many of the limitations of MetaPost derive from features of Metafont. For instance, MetaPost does not support all features of PostScript. Most notably, paths can have only one segment, and regions can be filled only with uniform colours. PostScript level 1 supports tiled patterns and PostScript 3 supports Gouraud shading.

Availability and usage

MetaPost is distributed with many distributions of the TeX and Metafont framework, for example, it is included in the MiKTeX and the TeX Live distributions.
The encapsulated postscript produced by Metapost can be included in LaTeX, ConTeXt, and TeX documents via standard graphics inclusion commands. The encapsulated postscript output can also be used with the PDFTeX engine, thus directly giving PDF. This ability is implemented in ConTeXt and in the LaTeX graphics package, and can be used from plain TeX via the supp-pdf.tex macro file.
ConTeXt and LuaTeX supports the inclusion of MetaPost code within the input file. Inclusion of MetaPost code in LaTeX is also possible by using LaTeX-packages, for example gmp or mpgraphics.

Examples

This is a single file example.mp which when processed by the MetaPost interpreter produces three eps files example.1, example.2, example.3. These are pictured on the right.

transform pagecoords;
pagecoords:=identity scaled 10mm shifted ;
beginfig
fill ----------
transformed pagecoords withcolor green;
draw........)
transformed pagecoords;
drawarrow --) transformed pagecoords;
endfig;
beginfig
draw
transformed pagecoords;
endfig;
pagecoords:=identity scaled 15mm shifted ;
beginfig ;
% declare paths to be used
path p,pt;
% set up points by defining relationships
z1=; z2=z1+2up;
z3=z1+whatever*dir =z2+whatever*dir ;
z4=z3+;
z5=z1+dir ;
z0=whatever=whatever;
% set up paths
p0=fullcircle yscaled.5 rotated 45 shifted z0 ;
p1=z2---z4..z0..z3---z1;
p2=p1 cutbefore p0 cutafter p0;
p3=p0 cutbefore p1 cutafter p1;
p4=p2---p3---cycle;
% define transformed versions of paths and points
for i=0 upto 4: pt=p transformed pagecoords; endfor
for i=0 upto 5: zt=z transformed pagecoords; endfor
% do some drawing
fill p4t withcolor ;
draw z1t---z2t withcolor.5white;
draw z3t---z4t withcolor.5white;
pickup pencircle;
draw p0t dashed withdots scaled.3;
draw p1t dashed evenly;
draw p2t withcolor blue;
draw p3t withcolor red;
label.lrt ;
label.llft ;
label.top ;
label.rt ;
label.llft ;
for i=0 upto 4:
drawdot zt withpen pencircle scaled 2;
endfor
endfig;
bye

The resulting three eps files can be used in TeX via LaTeX's \includegraphics command, ConTeXt's \externalfigure, Plain TeX's \epsfbox command, or the \convertMPtoPDF command from supp-pdf.tex. To view or print the third diagram, this inclusion is necessary, as the TeX fonts are not included in the eps files produced by MetaPost by default.