Go back to Alan's home page


  1. Define functions

    The following code defines a function to draw a circle, and returns the circle.

    vardef drawCircle(expr c,r)=
     path thecircle;
     thecircle = fullcircle scaled (2*r) shifted c;
     draw thecircle;
     thecircle
    enddef;

    Note that the last statement has no semicolon; its value is returned by the function.

  2. Differences btween vardef and def

    One difference is that, vardef has begingroup and endgroup automatically inserted. Between begingroup and endgroup, you can use save to make variables local, but not in def:

    vardef f=
    save a; numeric a; a:=2; show a; % 2
    enddef;

    def g=
    save a; numeric a; a:=2; show a; % 2
    enddef;

    beginfig(1)
    numeric a;
    a:=1; f; show a; % 1
    a:=1; g; show a; % 2, 'save' in def didn't proect a
    endfig;

    Another difference is that, vardef allows generic subsrcipts and multi-token in the name. For example, the following code is ok:

    vardef a[]b= enddef; % do nothing
    beginfig(1)
     numeric a[],b;
     a1=1; b=0; a1b;
    endfig;

    but

    def a[]b= enddef; % do nothing
    beginfig(1)
     numeric a[],b;
     a1=1; b=0; a1b;
    endfig;

    won't compile. See more details in the User's Manual.

  3. Use LaTeX formulas in labels

    Add the following lines at the beginning of your .mp file:

    verbatimtex
    %&latex
    \documentclass{article}
    \begin{document}
    etex

    Then you can do something like

    label.bot(btex $\frac{x}{a+b}$ etex, z1);

  4. Problem with fonts/labels

    When using labels, the ouput will not carry much font information. As a result it can not be (properly) viewed, neither can the dvi when compiled with latex. The labels can be seen when converted to pdf, or complied with pdflatex. This problem can be solved by setting the variable prologues to 2 or 3.

    prologues:=0; default. No embedded fonts in outputs.
    prologues:=1; deprecated. Output is "structured PostScript" (EPSF), but not completely conformant.
    prologues:=2; recommended. Fonts assumed from "environment".
    prologues:=3; standalone. Fonts embedded in output.

    For more information, see the User's Manual.