From Wiki
Jump to navigation Jump to search

MetaPost is a graphical programming language, based on Donald Knuth's MetaFont. Normally MP graphics are converted to PostScript and used with dvips, but ConTeXt can use it directly with PDF (see the MP to PDF manual).

MetaPost is ConTeXt's native graphics language. MetaFun - MetaPost in ConTeXt is a MetaPost module by Hans Hagen that adds a lot of extra features; it is enabled by default, so one could say that MetaFun is ConTeXt's default dialect of MetaPost.

Documentation & Tutorials

Using MetaPost in ConTeXt

With \startuseMPgraphic, you define a piece of graphics code that is processed anew every time the graphic is placed with \useMPgraphic. Further commands are described at MetaFun - MetaPost in ConTeXt.


  fill fullcircle scaled 20pt withcolor \mycolor;

red: \useMPgraphic{name}

blue: \useMPgraphic{name}

Different Packages, Extensions & Applications of Metapost

  • finomaton - drawing finite state automata
  • statsmac - metapost macros for statistics graphs
  • MetaUML - MetaPost library for typesetting UML diagrams
  • METAGRAPH - drawing (un)directed graphs

3D support

TODO: needs major review (See: To-Do List)

MetaPost relatives

Font Creation

3D drawing

  • Asymptote - inspired by MetaPost & fully generalizes MetaPost path construction algorithms to three-dimensions

Testing plain MetaPost

To test whether MetaPost is installed on your system, create a file called

  draw fullcircle scaled 3cm ;
endfig ;
end ;
  • apply MetaPost on the testfile
  • that should create a postscript file test.1
  • open test.1 with a postscript viewer like Ghostview, Okular, … You should see a circle.

Testing MetaPost embedded in ConTeXt

If MetaPost is installed on your system and working correctly, you can

  draw fullcircle scaled 3cm;
  • for mkii you need to have write18 support enabled and run
texexec --pdf test.tex
  • for mkiv run
context test.tex
  • that should create a pdf file test.pdf
  • open test.pdf with a pdf viewer like Adobe Acrobat, Okular... You should see a circle.

Text in MetaPost

Text that is typeset using textext or btex … etex adapts to the body font. However, when label("Foo", pair) or "Foo" infont defaultfont is used, which is not the recommended way, the text is typeset using the font MetafunDefault, which can be changed if desired. Example:

     label("Foo", origin);
     draw textext("Bar") yshifted -LineHeight;


  \switchtobodyfont [pagella, 18pt]

     label("Foo", origin);
     draw textext("Foo") yshifted -LineHeight;


The recommended way to typeset text is to use the textext macro and change the font using \setupMPinstance.

Repetition of a Text Element to Fill a Bar Using textext

Imagine you want to create a frame which consists out of one glyph or a combination of different glyphs in order to make a decorative border. For this purpose a series of such elements should be placed on the bar as pictures, evenly spaced. Probably you want to have at the end of the bar a different glyph inserted as a picture too.

The following code is working but the last picture is not the expected glyph. One would expect 14 times the "?" and at the end of the bar a "*":

      picture PicA ; PicA := textext("*");
      picture PicB ; PicB := textext("?");

      numeric Step ; Step := 2cm div bbwidth(PicB)  ;

      for i = 1 upto Step :
         draw PicB shifted (i*5mm,0) ;
      endfor ;
      draw PicA ;


What you get is 15 "?" but no "*"!

In order to get this corrected one needs to calculate the boundingbox of the "?" on beforehand. Hans Hagen provided this solution on 14-10-2014.

       path PicX ; PicX := boundingbox textext("?") ;

      picture PicA ; PicA := textext("*");
      picture PicB ; PicB := textext("?");

      numeric Step ; Step := 2cm div bbwidth(PicX) ;
      % numeric Step ; Step := 2cm div bbwidth(PicB)  ;

      for i = 1 upto Step :
         draw PicB shifted (i*5mm,0) ;
      endfor ;
      draw PicA ;


Now you get 14 "?" and the "*".

A Full Example

\setupbodyfont [8pt]





% ======== Definition of the frame in Metafun ==========
   path PicX ; PicX := boundingbox textext("= : =") ;
   numeric u,v,xshift,yshift,picwidth,remnant,nelements;

   u := OverlayWidth;
   v := OverlayHeight;
   path p,w;
   p := unitsquare xscaled u yscaled v;
   w := fullcircle scaled 8pt;
   picture Edgepic,Linepic[];
   color edgedotcolor,linelementcolorA;
   edgedotcolor       := blue;
   linelementcolorA := red;

   Linepic[1] := textext("\rotate[rotation=90]{= : =}");
   Linepic[2] :=  textext("= : =");
   Edgepic    := textext("o");
   %Calculations for the horizontal frame-element placement:
     % In order to make this work, the width of the line element needs to be calculated on beforehand.
     % path PicX; PicX := textext(")(");
   picwidth    := bbwidth(PicX);
   nelements := u div picwidth;
   remnant    := u mod picwidth;
   xshift        := picwidth + remnant/nelements;	 

   Linepic[3] := Linepic[2] shifted (0.5*picwidth,0);

   for a = llcorner p, ulcorner p :
      for i = 1 upto nelements :
         draw Linepic[3] shifted ((xpart a + (i-1)*xshift),ypart a) withcolor linelementcolorA;
   %Calculations for the vertical frame-element placement:
   picheight   := bbwidth(PicX);
   nelements := v div picheight;
   remnant    := v mod picheight;
   yshift        := picheight + remnant/nelements;
   Linepic[4]:= Linepic[1] shifted (0,0.5*picheight);
   for a = llcorner p, lrcorner p :
      for i = 1 upto nelements:
         draw Linepic[4] shifted (xpart a,ypart a + (i-1)*yshift) withcolor linelementcolorA;

   for i = llcorner p, lrcorner p,ulcorner p,urcorner p :
      fill w shifted (xpart i,ypart i) withcolor white;
        draw Edgepic shifted (xpart i,ypart i) withcolor edgedotcolor;

% For frame-element positioning checking:
%   draw p;

% ==========

   [width=0.8\textwidth,height=0.8\textheight,frame=off,background=Myframe, align={middle,lohi}]
      The butterfly counts not months but moments, and has time enough.
      {\tfx Rabindranath Tagore}


There is one important remark: The MPcalculation and the drawing itself must be inside \starttext ... \stoptext.

Layer sets as individual pages

The following ConTeXt code example can be used to create a series of layered Metapost graphics and generate multiple pages with different combinations of these layers. Each page represents a different composition of the defined layers. Use cases are when you need to compose manually your layer sets.

Base or Invisible Layer

The code begins by defining a base or invisible layer called "base" using the \startuseMPgraphic command. In this layer, various components are defined, such as paths, colors, and coordinates. These components will be used to draw the images in subsequent layers, but they're not drawn yet, thus this layer is "invisible".

Layer Definitions

After defining the base layer, several other layers are defined using the \startuseMPgraphic command. Each layer is given a unique name, in our example the names are such as "layerA" "layerW" or "layerM". These layers are created as images using the picture environment. Each image consists of drawn elements. In our example, the elements are filled squares with a specific color and a label.

Finishing Layer

The purpose of the finishing layer in our ConTeXt code is to add the final touches to the drawn layers and to clip the graphic to the same frame. It also includes the addition of page numbering to each page with the help of a Metapost variable, in our case "mypagenumber". We need this method because apparently, it is not possible to use the ConTeXt page numbering on a Metapost page. The finishing layer needs to be applied after drawing all the other layers.

Layer Sets

The before defined layers can be "set" (selected and combined) manually together, to become a layer set. They are layered on top of each other according to the order of appearance. The last layer is put on top of (and might cover) the previous one. Some subgroups or intermediate results of our "setting" or "putting together" will be reused again during the process. Selected intermediate results will be shown as such on individual pages, like a photo documentation of your setting procedure. Other intermediate results will be reused without ever being shown on an individual page. To be spared from doing such steps over and over again, they are stored, either as layersetpage plus a number, as a mere layerset, or as a layer subset.

All of them are defined using the \startuseMPgraphic command. Each of them includes a specific set of the previously defined layers (or if you want, also drawn elements, as the "special!" label in layersetpage4). The layersets which are shown as a page are numbered, starting from one.

Drawing the Pages

Finally, the code uses a loop to draw each layerset page onto its own page using the \dorecurse command. The loop iterates from 1 to a manually defined number, and for each iteration, it uses the \useMPgraphic command to draw the corresponding layersetpage, to which it is also passing the Metapost variable "mypagenumber".

The resulting output is a series of pages, each displaying a different selection and combination of the defined layers.

\startuseMPgraphic{base} %Here in the base, we define the components we will use to draw our images
numeric u; u = 1cm; 
numeric framefactor; framefactor = 3.4;
path TheFrame ; TheFrame := fullsquare xyscaled (framefactor*u, framefactor*u)  ; %Needed to have for every page the same dimensions. Use (sqrt(2)*framefactor*u, framefactor*u) for DIN A Landscape ratio of the dimensions. 
color stone_color; stone_color = 1/256(199,199,199);
color light_teal_color; light_teal_color = 1/256(154,209,189);

z1 = (-u,-u) ;
z2 = (u,-u) ;
z3 = (-u,u) ;
z4 = (u,u) ;
z5 = (-.5u,0);
z6 = (.8u,0);

picture layerA;
fill fullsquare scaled u shifted(z1) withcolor stone_color;
label("A", z1);

picture layerW;
fill fullsquare scaled u shifted(z2) withcolor stone_color;
label("W", z2);

picture layerM;
fill fullsquare scaled u shifted(z3) withcolor blue;
label("M", z3) withcolor white;

picture layerC;
fill fullsquare scaled 1.5u shifted(z4) withcolor  transparent(1, .8, green);
label("C", z4);

picture layerY;
fill fullsquare scaled 2u shifted(z5) withcolor light_teal_color;
label("Y", z5);

picture layerU;
fill fullsquare rotated 30 scaled 2u shifted(z4) withcolor transparent(1, .5, red);
label("U", z4);

picture layerV;
fill fullsquare rotated 10 scaled 1.4u shifted(z6) withcolor transparent(1, .7, yellow);
label("V", z6);
\stopuseMPgraphic %base

\startuseMPgraphic{finishing} %This is needed to clip the graphic to the same frame and to add the page numbering. Both have to happen after drawing the layers. 
draw TheFrame withpen pencircle scaled .25u withcolor magenta ;
draw textext(decimal(\MPvar{mypagenumber})) scaled .1u withcolor magenta ;
setbounds currentpicture to TheFrame ;
%scaling the page to your preferred dimension: 
desiredformat := 150;% In pixels (will be converted by default at 72dpi). The shorter dimension of DIN A4 would be 595 pixels. 
currentpicture := currentpicture xysized (desiredformat ,desiredformat ); %for square pages. If DIN A Landscape, use  (sqrt(2)*desiredformat ,desiredformat)
\stopuseMPgraphic %finishing

\startuseMPgraphic{Gamma} %This is a layer subset which will be used more than once. I call it a layer subset and not a layer set because it doesn't include another MPgraphic.
draw layerU ;
draw layerM ;
\stopuseMPgraphic %Gamma

\startuseMPgraphic{layersetpage1} %Each layersetpage will become a page containing the defined layer set. The number corresponds to the page number. Start with number 1. 
\includeMPgraphic{base} %base has to be included only in layersetpage1. It will be passed on to the next layersetpages via inclusion. 
draw layerW ;
draw layerA ;

\startuseMPgraphic{layersetpage2} %There should be no gap in the sequence of the numbers of the layersetpages
\includeMPgraphic{layersetpage1} ;
draw layerV ;

\includeMPgraphic{layersetpage2} ;
\includeMPgraphic{Gamma} ;

\startuseMPgraphic{layersetpre4} %This is a layer set because it includes another MPgraphic. It will be used more than once, but it will not become a page itself. It's only an intermediate step on the way to the next layersetpage. For that reason, it's not named layersetpage. Because it's coming before the layersetpage4, I call it layersetpre4. If I need another intermediate step stored before layersetpre4, I would call it layersetprepre4. But that's only a naming convention for myself.
\includeMPgraphic{layersetpage1} ;
draw layerY ;

\includeMPgraphic{layersetpre4} ;
label("special!", (z3+z4)/2) rotated 5 withcolor red;

\includeMPgraphic{layersetpage3} ;
draw layerC ;

\includeMPgraphic{layersetpre4} ;
draw layerC ;


\dorecurse{6}{ %This loop starts from 1 until the set number and draws each layersetpage onto its own page. Don't set a number which is higher than the number of your last layersetpage.

%By differentiating finishinglayersetpage#1 from layersetpage#1, we can avoid adding \includeMPgraphic{finishing} to each \startuseMPgraphic{layersetpage}. This allows us to apply the scaling of the page to our desiredformat, because the scaling will done only once per \useMPgraphic{}.
\includeMPgraphic{layersetpage#1} ;

%uncomment if needed: 
% \setupTEXpage[background=color, backgroundcolor=gray]
  \startTEXpage[pagestate=start] %`pagestate=start` is needed to have incremented page numbering of the pdf

layer sets.png

Other Links