Difference between revisions of "MetaPost"

From Wiki
Jump to navigation Jump to search
(Make clearer the MetaPost/MetaFun relation)
(Add examples)
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]
  
 +
== Using MetaPost in ConTeXt ==
  
== Troubleshooting plain MetaPost ==
+
With {{cmd|startuseMPgraphic}}, you define a piece of graphics code that is processed anew every time the graphic is placed with {{cmd|useMPgraphic}}.
* Test if MetaPost is installed on your system.
 
* create test.mp containing this:
 
  
beginfig(1)
+
<context>
  draw fullcircle scaled 3cm ;
+
\def\mycolor{.625red}
endfig ;
+
 
end ;
+
\startuseMPgraphic{name}
 +
  fill fullcircle scaled 20pt withcolor \mycolor;
 +
\stopuseMPgraphic
 +
 
 +
red: \useMPgraphic{name}
  
* apply MetaPost on the testfile
+
\def\mycolor{.625blue}
 +
blue: \useMPgraphic{name}
 +
</context>
  
mp test.mp
+
For graphics that are the same every time, it is better to use {{cmd|startreuseableMPgraphic}}: the graphic is compiled only once, and the one resulting picture can be placed at various points with {{cmd|reuseMPgraphic}}.
or
 
mpost test.mp
 
  
* that should create a postscript file test.1
+
<context>
* open test.1 with a postscript viewer like ghostview, okular, … You should see a circle.
+
\def\mycolor{.625red}
  
== Troubleshooting MetaPost embedded in ConTeXt ==
+
\startreusableMPgraphic{name}
 +
  fill fullcircle scaled 20pt withcolor \mycolor;
 +
\stopreusableMPgraphic
  
* create test.tex containing this:
+
red: \reuseMPgraphic{name}
  
\starttext
+
\def\mycolor{.625blue}
\startMPcode
+
blue: \reuseMPgraphic{name}
  draw fullcircle scaled 3cm;
+
</context>
\stopMPcode
 
\stoptext
 
  
* for mkii you need to have write18 support enabled and run
+
When reusing a graphic is not important, you can simply use {{cmd|startMPcode}}.
texexec --pdf test.tex
 
  
* for mkiv run
+
<context>
context test.tex
+
\startMPcode
 +
  fill fullcircle scaled 20pt withcolor .625green;
 +
\stopMPcode
 +
</context>
  
* that should create a pdf file test.pdf
+
See also section 3.3 of [[manual:metafun-s.pdf|MetaFun manual]].
* open test.pdf with a pdf viewer like acroread, okular... You should see a circle.
 
  
 
== Different Packages, Extensions & Applications of Metapost ==
 
== Different Packages, Extensions & Applications of Metapost ==
Line 74: Line 78:
 
* [http://asymptote.sourceforge.net/ Asymptote] - inspired by MetaPost & fully generalizes MetaPost path construction algorithms to three-dimensions
 
* [http://asymptote.sourceforge.net/ Asymptote] - inspired by MetaPost & fully generalizes MetaPost path construction algorithms to three-dimensions
  
== Using MP in ConTeXt ==
+
== Testing plain MetaPost ==
The first way is a usable graphic. Such a graphic is calculated anew each time it is used. An example:
+
To test whether MetaPost is installed on your system, create a file called {{code|test.mp}}:
  
<texcode>
+
beginfig(1)
\startuseMPgraphic{name}
+
  draw fullcircle scaled 3cm ;
  fill fullcircle scaled 5cm withcolor red;
+
endfig ;
\stopuseMPgraphic
+
end ;
  
\useMPgraphic{name}
+
* apply MetaPost on the testfile
</texcode>
 
  
As said, this graphic is calculated each time it is placed, which can be time consuming. Apart from the time aspect, this also means that the graphic itself is incorporated many times. Therefore, for graphics that don’t change, CONTEXT provides reusable graphics:
+
mp test.mp
 +
or
 +
mpost test.mp
  
<texcode>
+
* that should create a postscript file test.1
\startreusableMPgraphic{name}
+
* open test.1 with a postscript viewer like Ghostview, Okular, … You should see a circle.
  fill fullcircle scaled 200pt withcolor .625yellow;
 
\stopreusableMPgraphic
 
  
\reuseMPgraphic{name}
+
== Testing MetaPost embedded in ConTeXt ==
</texcode>
 
  
When reusing a graphic is not so important and you just want to include something inline, you can also use the following:
+
If MetaPost is installed on your system and working correctly, you can  
  
<texcode>
+
\starttext
\startMPcode
+
\startMPcode
  fill fullcircle scaled 200pt withcolor .625yellow;
+
  draw fullcircle scaled 3cm;
\stopMPcode
+
\stopMPcode
</texcode>
+
\stoptext
  
See also section 3.3 of [[manual:metafun-s.pdf|MetaFun manual]].
+
* for mkii you need to have write18 support enabled and run
 +
texexec --pdf test.tex
  
=== Examples ===
+
* for mkiv run
 +
context test.tex
  
{{todo|Enable support for Metapost code when running ConTeXt online. This will allow easy creation of Metapost examples, including both source code and resulted figure.}}
+
* that should create a pdf file test.pdf
 
+
* open test.pdf with a pdf viewer like Adobe Acrobat, Okular... You should see a circle.
Currently, running online Metapost code, like this example, does not return anything.
 
 
 
<pre>
 
<context source="yes" text="produces">
 
\startMPcode
 
  fill fullcircle scaled 200pt withcolor .625yellow;
 
\stopMPcode
 
</context>
 
</pre>
 
  
 
== Other Links ==
 
== Other Links ==

Revision as of 13:17, 26 November 2012

< 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.

For graphics that are the same every time, it is better to use \startreuseableMPgraphic: the graphic is compiled only once, and the one resulting picture can be placed at various points with \reuseMPgraphic.

When reusing a graphic is not important, you can simply use \startMPcode.

See also section 3.3 of MetaFun manual.

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.

Other Links