MetaFun - MetaPost in ConTeXt

From Wiki
Jump to navigation Jump to search

< Graphics|Metafun|Metapost>

ConTeXT provides a tight integration of TeX and Metapost. Metapost can used behind the scenes for some graphic trickery. This integration is also helpful for drawing figures in metapost and use them in the document. Some of the commonly used commands for this are


\startMPenvironment

This is used to change the body font for metapost graphics. For example, to get the text in metapost graphics in times, use

\startMPenvironment
\usetypescript[times][texnansi]
\switchtobodyfont[times,10pt]
\stopMPenvironment

\startMPinclusions

This can be used for specifying metapost files to input and also to define metapost functions. For example

\startMPinclusions
 input boxes ;
 
 vardef my_metapost_fun(expr var)=
 ...
 enddef ;
\stopMPinclusions


\startMPpage

This produces a page containing containing metapost graphic. The resulting pdf is self contained (it has all the fonts embedded) This is useful if you want to send the figure to a coauthor or a journal. For example, one can have

\starttext
\startMPpage
 draw fullcircle scaled 2cm ;
 dotlabel.bot(textext("(0,0)"),origin) ;
\stopMPpage

\startMPpage
 draw fullcircle scaled 2cm ;
 drawarrow (0,0)--(1cm,0) ;
 label.bot(textext("$r$"),(5mm,0)) ;
\stopMPpage

\stoptext

Processing this file using

 texmfstart texexec --pdf filename

gives a pdf with two pages of graphics.

\startstaticMPfigure

\startuseMPgraphic series of commands were designed for using metapost for drawing fancy frames and backgrounds and not for using stand-alone graphics for a document. \startstaticMPfigure creates a separate pdf file containing the graphic. This pdf can be included in the document using \usestaticMPfigure. Context is clever enough to recompile the resulting metapost file, only if something inside the start stop staticMPgraphic has changed.

For example,

\startstaticMPfigure{center}
 draw fullcircle scaled 2cm ;
 dotlabel.bot(textext("(0,0)"),origin) ;
\stopstaticMPfigure

\startstaticMPfigure{radius}
 draw fullcircle scaled 2cm ;
 drawarrow (0,0)--(1cm,0) ;
 label.bot(textext("$r$"),(5mm,0)) ;
\stopstaticMPfigure

\starttext

Circle centered \usestaticMPfigure[center][width=1cm] at origin with radius
\usestaticMPfigure[radius][width=1cm] $r=1\,\text{cm}$.

\stoptext
  • center and radius above are labels that are used to access the figure.
  • \usestaticMPfigure takes the first argument as the label of the figure to be inserted. The second argument has the same options as the second argument of \externalfigure
  • To force the figure files to be regenerated, remove all the *.mp.md5 files from the current directory.

Transparent colors in figures

You can use transparent colors! For example:

\runMPgraphicstrue
\setupcolors[state=start]
\starttext
\startreusableMPgraphic{a}
fill unitsquare scaled 1cm withcolor yellow;
fill unitsquare shifted (0.5,0.5)
  scaled 1cm withcolor transparent(1,0.5,red);
\stopreusableMPgraphic

\placefigure[force,none]{}{\reuseMPgraphic{a}}
\stoptext

If you see black squares rather than a yellow one underneath a partially transparent red one, then you probably hit the 'missing specials' problem diagnosed by Taco (ntg-context list, 23 Sep 2006 22:00:42 +0200):

This all sounds like the 'missing specials' problem that is caused
by conflicting -progname= arguments when using the web2c version
of metapost.

Make sure you do not have conflicting memory settings for both
   main_memory.mpost
as well as
   main_memory.metafun

The best is to remove all trace of '.mpost' and '.metafun' memory
settings from your texmf.cnf, but at least make sure all the
'.mpost' and 'metafun' values are the same .

Then regenerate metafun using texexec --make, and all should be
well again.

And indeed it fixed it for me (Sanjoy). These were the memory setting in the /etc/texmf/texmf.d/95NonPath.cnf configlet that is part of Debian and Ubuntu distributions of TeX:

main_memory = 1000000 % words of inimemory available; also applies to inimf&mp
main_memory.context = 1500000
main_memory.mpost = 1500000

I commented out the .mpost line (there was no separate metafun line, which I guess instead used the main_memory value of 1000000), regenerated /etc/texmf/texmf.cnf with update-texmf (as root), then regenerated metafun with texexec --make metafun (as me) and transparency worked.