Practical: Metapost
Practical: Metapost
SUPELEC –
metapost
Campus de Metz
2, rue E. Belin
F--57070 Metz
Practical METAPOST 1
[email protected]
abstract
In this article, I will explain how to pratically use metapost. This program is very different from
usual drawing programs, but it fits very well in a tex based typesetting system.
résumé
Ces quelques pages ont pour objectif de montrer comment utiliser pratiquement metapost. Ce
programme est très différent des logiciels classique de dessin, mais s’intègre extrêmement bien
dans la chaı̂ne de composition basée sur tex.
samenvatting
De volgende pagina’s laten zien hoe metapost praktisch kan worden gebruikt. Dit programma
verschilt sterk van de klassieke tekenprogramma’s, maar laat zich voortreffelijk combineren met
op tex gebaseerde tekstverwerkingssystemen.
1. First published in Cahiers GUTenberg n o 41 — Novembre 2001. Authorized translation from the
French by Karel and Hanny Wesseling.
80 MAPS
Practical metapost metapost
.3
.2
METAPOST metademo.1
metademo.mp
TeX
mpto
dvitomp
metademo.mpx
for n = 1 upto 4:
beginfig(n) ;
pickup pencircle scaled 2mm ; star(2cm,n+n+3,origin,c[n]) ;
15 endfig ;
endfor ;
end
Compilation of this file with mpost produces a series of four postscript output files, each
containing one of the four declared figures (1) through (4) as shown below:
c:\>mpost metademo.mp
This is MetaPost, Version 0.641 (Web2c 7.3.3.1)
(metademo.mp [1] [2] [3] [4] )
4 output files written: metademo.1 .. metademo.4
Transcript written on metademo.log.
It is now time to include the figures just generated in your .tex document. This is simple
when you are using a recent latex version in which the graphicx package treats the files
output by metapost as if they were Postscript.
Najaar 2002 81
metapost Fabrice Popineau
For older versions you might consider renaming the files such that the file exten-
sion becomes .eps. For example, rename metademo.1 to metadem1.eps. These files
can then be handled without problems. Perhaps a better solution is to add a line
\DeclareGraphicsRule with parameters as shown in the latex document below. This
line specifies to latex that files included with the \includegraphics command that have
an extension that is not recognized should be treated as postscript files.
0 \documentclass[a4paper,11pt]{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=1.5cm]{metademo.1} \hfill
\includegraphics[width=1.5cm]{metademo.2} \hfill
\includegraphics[width=1.5cm]{metademo.3} \hfill
10 \includegraphics[width=1.5cm]{metademo.4}
\end{document}
Now compile this file and pass it to dvips to obtain a postscript file which looks like the
starry result below.
In its default configuration, metapost does not create encapsulated, i.e. self contained,
postscript but as long as you compile to DVI and use dvips exclusively to obtain a
postscript output file, there is no problem. But if you want to use the postscript generat-
ed by metapost with other applications such as Adobe Illustrator for example, you must
include in your .mp file a prologue variable which is given the value 2, as we saw in the
metapost program above, as follows:
prologues := 2 ;
This variable is 0 by default. A positive value forces metapost to generate encapsu-
lated postscript. Use a value of 1 for troff, 2 for dvips. With a prologue value of
2, metapost will look for the file psfonts.map, the font map file that is usually read by
dvips. These mappings, however, are not included in the metapost output file. Thus,
they have to be supplied together with the output, unless they already are resident in the
target application. The immediate drawback of this approach is that you face problems as
soon as non--ASCII character codes are used. The code vectors assumed by dvips will
simply be ignored by other applications.
82 MAPS
Practical metapost metapost
The context system 2 contains a module supp-pdf that permits the direct conversion
by tex of eps files produced by metapost into pdf instructions. The advantage of this
module is that it is independent of context and can be used in other environments, for
example latex. When used in conjunction with pdftex, the graphicx package recog-
nizes .mps files containing metapost instructions and charges supp-pdf with the conver-
sion of such graphics.
However, metapost produces graphic output files with extensions .1, .2 and so on, so
we have to tell the graphicx package that such files are actually .mpsfiles. Therefore we
always add an IF statement as follows:
\usepackage{ifpdf}
...
\ifpdf
\DeclareGraphicsRule{*}{mps}{*}{*}
\else
% Recent LaTeX versions don’t require the next line
% \DeclareGraphicsRule{*}{eps}{*}{*}
\fi
Thus we see that metapost vector graphics files are compatible with postscript as well
as pdf output. The same source files serve two output formats, and there is no need to
keep and maintain two file versions, one for each output type.
context has an equally easy utility to achieve this called mptopdf. It is a Perl script
accompanied by a reduced format file mptopdf.efmt that automatically includes the nec-
essary commands when compiling metapost files through texexec, the context work
bench of many uses.
context also offers a new format for metapost called MetaFun which loads its pro-
prietary macros on top of those of metapost. They include flashy effects such as the one
demonstrated below (figure 2): a graded background to a line of text, going from red to
yellow and back to red again.
Circular shade background
Figure 2 MetaFun’s
shady background.
2. context, whose principal author is Hans Hagen, is a complete text composition system more
modern and more ambitious than latex. For more information on context look at the NTG Web
site www.ntg.nl/context/
Najaar 2002 83
metapost Fabrice Popineau
0 \definecolor[a][yellow]
\definecolor[c][darkred]
\startuniqueMPgraphic{CircularShade}
path p ;
5 p := unitsquare xscaled \overlaywidth
yscaled \overlayheight ;
circular_shade(p,0,\MPcolor{a},\MPcolor{c}) ;
\stopuniqueMPgraphic
10 \defineoverlay[circular shade]
[\uniqueMPgraphic{CircularShade}]
\framed[background=circular shade,frame=off]
{\bf Circular shade background}
\ifpdf
5 \DeclareGraphicsRule{*}{mps}{*}{}
\fi
\begin{document}
84 MAPS
Practical metapost metapost
\end{document}
Unfortunately, and unlike in context, the emp package does not allow the exchange of
parameters between tex and metapost.
Conclusion
I hope that with this publication I have eliminated most of the practical difficulties that
one may meet initially when attempting to use metapost. I also hope to have induced
you to undergo the joys of geometry graphics. Bonne chance. Good luck.
Najaar 2002 85