Difference between revisions of "MetaPost"

From Wiki
Jump to navigation Jump to search
(Add “Text in MetaPost” section)
Line 13: Line 13:
 
* [http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html Lots of examples]
 
* [http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html Lots of examples]
 
* [[Color in MetaPost]] describes how to get color mixtures, TeX colors, and transparent effects in MetaPost graphics.
 
* [[Color in MetaPost]] describes how to get color mixtures, TeX colors, and transparent effects in MetaPost graphics.
 +
* [http://wiki.contextgarden.net/MetaPost_in_ConTeXt MetaPost in ConTeXt]
  
 
== Using MetaPost in ConTeXt ==
 
== Using MetaPost in ConTeXt ==
Line 89: Line 90:
 
* that should create a pdf file test.pdf
 
* that should create a pdf file test.pdf
 
* open test.pdf with a pdf viewer like Adobe Acrobat, Okular... You should see a circle.
 
* 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:
 +
 +
<context mode="mkiv" source="yes" text="Gives:">
 +
\starttext
 +
  \startMPcode
 +
    label("Foo", origin);
 +
    draw textext("Bar") yshifted -LineHeight;
 +
  \stopMPcode
 +
 +
  \definefontsynonym
 +
    [MetafunDefault]
 +
    [Bold*default]
 +
 +
  \switchtobodyfont [pagella, 18pt]
 +
 +
  \startMPcode
 +
    label("Foo", origin);
 +
    draw textext("Foo") yshifted -LineHeight;
 +
  \stopMPcode
 +
\stoptext
 +
</context>
 +
 +
The recommended way to typeset text is to use the `textext` macro and change the font using {{cmd |setupMPinstance}}.
  
 
== Other Links ==
 
== Other Links ==

Revision as of 21:18, 29 August 2013

< Math, MetaFun, Graphics

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 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 MetaPost in ConTeXt.

\def\mycolor{.625red}

\startuseMPgraphic{name}
  fill fullcircle scaled 20pt withcolor \mycolor;
\stopuseMPgraphic

red: \useMPgraphic{name}

\def\mycolor{.625blue}
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 test.mp:

beginfig(1)
  draw fullcircle scaled 3cm ;
endfig ;
end ;
  • apply MetaPost on the testfile
mp test.mp
or
mpost test.mp
  • 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

\starttext
\startMPcode
  draw fullcircle scaled 3cm;
\stopMPcode
\stoptext
  • 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:

\starttext
  \startMPcode
     label("Foo", origin);
     draw textext("Bar") yshifted -LineHeight;
  \stopMPcode

  \definefontsynonym
    [MetafunDefault]
    [Bold*default]

  \switchtobodyfont [pagella, 18pt]

  \startMPcode
     label("Foo", origin);
     draw textext("Foo") yshifted -LineHeight;
  \stopMPcode
\stoptext

Gives:

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

Other Links