Difference between revisions of "MetaFun - MetaPost in ConTeXt"

From Wiki
Jump to navigation Jump to search
(Less <cmd>, more {{cmd|...}})
m (fix pragma links & typos)
 
(25 intermediate revisions by 5 users not shown)
Line 1: Line 1:
< [[Graphics]]|[[Metafun]]|[[Metapost]]>
+
__TOC__
  
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
 
  
== \defineMPinstance and \setupMPinstance ==
+
MetaFun is ConTeXt’s extension of the [[MetaPost]] language. It provides a tight integration of both 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 a document.
  
{{cmd|defineMPinstance}} and {{cmd|setupMPinstance}} can be used to set up MetaPost invocations with particular features. One use case could be to select a sans serif font for all MetaPost
+
'''The [[manual:metafun-p.pdf|MetaFun manual]]''', also available in a [[manual:metafun-s.pdf|screen version]].
labels. Example:
 
  
<texcode>
 
\setupMPinstance [metafun] [textstyle=sans]
 
\starttext
 
  
\startMPcode
+
Don’t miss the illustrations at 100% MetaFun page [https://art-aleatoire.com art-aleatoire].
  draw textext("some text") ;
 
\stopMPcode
 
  
\stoptext
 
</texcode>
 
  
Another use case could be to create a custom setup with important text in bold and red for
+
This page is a brief overview of some of the commonly used commands.
selected MetaPost graphics. Example:
 
  
<texcode>
+
= ConTeXt System Overview =
\defineMPinstance
 
  [important]
 
  [metafun] % needs to be inherited from the global metafun instance
 
  [textcolor=red]
 
  
\defineMPinstance
+
[[File:ConTeXt-System.png|700px]]
  [veryimportant]
 
  [important]
 
  [textstyle=bold]
 
  
\starttext
+
= Commonly used commands =
  
% normal black text
+
; {{cmd|defineMPinstance}}, {{cmd|setupMPinstance}}: used to set up MetaPost invocations with particular features. One use case could be to select a sans serif font for all MetaPost labels.
\startMPcode
+
: {{cmd|startMPenvironment}} '''is DEPRECATED''' in which one could place settings that one only wanted to apply in the MetaPost environment, but not in the document itself. One should use {{cmd|defineMPinstance}} and {{cmd|setupMPinstance}}, instead.
  draw textext("some text") ;
 
\stopMPcode
 
  
% red text
+
; {{cmd|startMPinclusions}}: Inside, one can specify MetaPost files to ''input'', and also define MetaPost functions.
\startMPcode{important}
 
  draw textext("some text") ;
 
\stopMPcode
 
  
% red bold text
+
; {{cmd|startuseMPgraphic}}, {{cmd|useMPgraphic}}: defines a piece of graphics code that is processed anew every time the graphic is placed with {{cmd|useMPgraphic}}.
\startMPcode{veryimportant}
 
  draw textext("some text") ;
 
\stopMPcode
 
  
\stoptext
+
; {{cmd|startreusableMPgraphic}}, {{cmd|reuseMPgraphic}}: For graphics that are the same every time, it is better to use {{cmd|startreusableMPgraphic}}: the graphic is compiled only once, and the one resulting picture can be placed at various points with {{cmd|reuseMPgraphic}}.
</texcode>
 
  
 +
; {{cmd|startMPcode}}: When reusing a graphic is not important, you can simply use {{cmd|startMPcode}}.
  
=== \startMPenvironment ===
+
; {{cmd|startMPpage}}: produces a page a containing MetaPost graphic. The page is just large enough to contain the 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.
  
{{cmd|startMPenvironment}} is a '''deprecated''' evironment in which one could place settings that one only wanted to apply in the MetaPost environment, but not in the document itself. One should use {{cmd|defineMPinstance}} and {{cmd|setupMPinstance}}, instead.
+
; {{cmd|startstaticMPfigure}}, {{cmd|usestaticMPfigure}}: The {{cmd|startuseMPgraphic}} series of commands were designed for using MetaPost for drawing fancy frames and backgrounds and not for using <i>stand-alone</i> graphics for a document. {{cmd|startstaticMPfigure}} creates a separate pdf file containing the graphic. This pdf can be included in the document using {{cmd|usestaticMPfigure}}. ConTeXt is clever enough to recompile the resulting MetaPost file only if something inside the {{cmd|startstaticMPfigure}} has changed.
  
An example of its usage:
+
= Documentation and links =
  
<texcode>
+
* [[manual:mmakempy.pdf|MakeMPY]]: create MetaPost outlines (by Pragma)
\startMPenvironment
+
* [[Flow Charts|FlowCharts]]: encapsulated application, demonstration of MetaPost/MetaFun
\usetypescript[times][texnansi]
+
* [http://www.pragma-ade.nl/general/sources/mptopdf.pdf MPtoPDF]: create PDF from MetaPost (by Pragma)
\switchtobodyfont[times,10pt]
+
* [http://www.pragma-ade.nl/show-gra.pdf a few examples of MetaPost graphics]
\stopMPenvironment
+
* [http://www.pragma-ade.nl/articles/art-puzz.pdf Puzzling graphics in MetaPost] (article)
</texcode>
+
* [http://www.ntg.nl/maps/25/15.pdf metapost metafun. Hans Hagen at MAPS]
 +
* [[Fill Patterns|FillPatterns]]: Fill pattern examples using MetaPost/MetaFun
  
An MPenvironment is sort of like <b>verbatimtex..etex</b> in regular (standalone) MetaPost figures.  Here is an example of using verbatimtex in a regular MetaPost figure:
 
<texcode>
 
verbatimtex
 
% center-align and stack the two arguments
 
\def\2#1#2{\vbox{\halign{\hfil##\hfil\cr #1\cr #2\cr}}}
 
etex
 
  
beginfig(1)
+
----
  z0 = (2in,0);
+
[[Category:Graphics]]
  draw fullcircle scaled 3cm;
+
[[Category:Metapost]]
  label(btex \2{sun}{mass $M$} etex, origin);
 
  draw fullcircle scaled 1.5cm shifted z0;
 
  label(btex \2{earth}{mass $m$} etex, z0);
 
endfig;
 
end
 
</texcode>
 
 
 
To integrate such a graphic into a ConTeXt source file, there are several choices. 
 
 
 
1. Place the definition of <b>\2</b> in the MPenvironment.  However, that fails (as of the 2006.09.28 beta but it's likely to work soon, knowing Hans) because the definition is stored as a macro itself, and then the parameters #1 and #2 become ##1 and ##2, alas.  There is a <b>\startbuffer</b> trick that Hans posted on the mailing list.  But perhaps the easiest is:
 
 
 
2. Place the definition of <b>\2</b> in the ConTeXt environment, and change <b>btex..etex</b> to <b>\sometxt</b>.  Here's an example:
 
 
 
<texcode>
 
% in a large document, put this def in an environment
 
\def\2#1#2{\vbox{\halign{\hfil##\hfil\cr #1\cr #2\cr}}}
 
 
 
\starttext
 
 
 
% earth and sun
 
\startreusableMPgraphic{fig}
 
  z0 = (2in,0);
 
  draw fullcircle scaled 3cm;
 
  label(\sometxt{\2{sun}{mass $M$}}, origin);
 
  draw fullcircle scaled 1.5cm shifted z0;
 
  label(\sometxt{\2{earth}{mass $m$}}, z0);
 
\stopreusableMPgraphic
 
 
 
\reuseMPgraphic{fig}
 
 
 
\stoptext
 
</texcode>
 
 
 
Warning: <b>\sometxt</b> cannot be used in loops.  Instead use <b>\textext</b>.  [Someone who understands why should expand this statement!]
 
 
 
== \startMPinclusions ==
 
 
 
Inside {{cmd|startMPinclusions}} one can specify MetaPost files to ''input'', and also define MetaPost functions.
 
For example
 
<texcode>
 
\startMPinclusions
 
input boxes ;
 
 
vardef my_metapost_fun(expr var)=
 
...
 
enddef ;
 
\stopMPinclusions
 
</texcode>
 
 
 
 
 
== \startMPpage ==
 
 
 
{{cmd|startMPpage}} produces a page a containing MetaPost graphic. The
 
page is just large enough to contain the 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
 
 
 
<texcode>
 
\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
 
</texcode>
 
 
 
Processing this file using
 
<texcode>
 
texmfstart texexec --pdf filename
 
</texcode>
 
gives a pdf with two pages of graphics.
 
 
 
== \startstaticMPfigure ==
 
 
 
The {{cmd|startuseMPgraphic}} series of commands were designed for using MetaPost for drawing fancy frames and backgrounds and not for using <i>stand-alone</i> graphics for a document. {{cmd|startstaticMPfigure}} creates a separate pdf file containing the graphic. This pdf can be included in the document using {{cmd|usestaticMPfigure}}. ConTeXt is clever enough to recompile the resulting MetaPost file, only if something inside the start stop staticMPgraphic has changed.
 
 
 
For example,
 
<texcode>
 
\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
 
</texcode>
 
 
 
* '''center''' and '''radius''' above are ''labels'' that are used to access the figure.
 
* {{cmd|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 {{cmd|externalfigure}}
 
* To force the figure files to be regenerated, remove all the <code>*.mp.md5</code> files from the current directory.
 
==Transparent colors in figures==
 
 
 
You can use transparent colors!  For example:
 
 
 
<texcode>
 
\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
 
</texcode>
 
 
 
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):
 
 
 
<pre>
 
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.
 
</pre>
 
  
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:
 
  
<pre>
+
{{MetaPost see also}}
main_memory = 1000000 % words of inimemory available; also applies to inimf&mp
+
{{Metapost navbox}}
main_memory.context = 1500000
 
main_memory.mpost = 1500000
 
</pre>
 
 
 
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.
 
 
 
[[Category:Graphics]]
 
[[Category:Metapost]]
 

Latest revision as of 08:16, 1 July 2022


MetaFun is ConTeXt’s extension of the MetaPost language. It provides a tight integration of both 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 a document.

The MetaFun manual, also available in a screen version.


Don’t miss the illustrations at 100% MetaFun page art-aleatoire.


This page is a brief overview of some of the commonly used commands.

ConTeXt System Overview

ConTeXt-System.png

Commonly used commands

\defineMPinstance, \setupMPinstance
used to set up MetaPost invocations with particular features. One use case could be to select a sans serif font for all MetaPost labels.
\startMPenvironment is DEPRECATED in which one could place settings that one only wanted to apply in the MetaPost environment, but not in the document itself. One should use \defineMPinstance and \setupMPinstance, instead.
\startMPinclusions
Inside, one can specify MetaPost files to input, and also define MetaPost functions.
\startuseMPgraphic, \useMPgraphic
defines a piece of graphics code that is processed anew every time the graphic is placed with \useMPgraphic.
\startreusableMPgraphic, \reuseMPgraphic
For graphics that are the same every time, it is better to use \startreusableMPgraphic: the graphic is compiled only once, and the one resulting picture can be placed at various points with \reuseMPgraphic.
\startMPcode
When reusing a graphic is not important, you can simply use \startMPcode.
\startMPpage
produces a page a containing MetaPost graphic. The page is just large enough to contain the 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.
\startstaticMPfigure, \usestaticMPfigure
The \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 \startstaticMPfigure has changed.

Documentation and links