## Metapost – or how to code an image

What software do you use when you need to draw a nice image? I often need to draw things related to research and I never managed to efficiently use a software which uses the mouse or touchpad to draw and modify things. Moreover, if you need to add some mathematical text to the figure things get even more complicated. For me it is more natural to use code to generate graphics, if this is possible. Using something like Matlab to draw is possible. The advantage is that once you have a working code which produces what you want, you can easily modify it. If you have an image where you need to repeat things, using loops can facilitate the job (programmers will understand…).

This is were Metapost comes into play. I found this a long time ago while searching for a tool to build nice graphics for my master thesis. Being used to LaTeX for typesetting math, it was a natural way to draw using code. I do not claim it is the best or the simplest way, but for me it works. Most importantly, it allows me to build high quality, vectorized graphics, which are memory efficient. The advantage of vector graphics is that you can zoom as much as you want and you’ll never see the pixels.

There are a bunch of places where you can learn Metapost. I’ll put here some references I use a lot. The easiest way to start drawing in Metapost is to take a look at some existing codes and modify them. You can find lots of examples in this tutorial. If you have a linux system, you can compile metapost .mp files to obtain high quality pdfs using the command mptopdf. If not, then you can use the online Metapost editor found here. In any case, here are some of my codes for some drawings in Metapost.

Here is the code for my website logo: If you want to see the lossless vectorized pdf click to see the following file: logo-0 You can see that it has a border which changes from one color to another. This was done using a loop and a parameter to vary the color as we go along the boundary.

prologues:=3; verbatimtex %&latex \documentclass{minimal} \begin{document} etex beginfig(0); % copy from here to use the online previewer u:=25; % 25 = 25bp = 25 PostScript points = 25/72 in wi:=10; % width in units u he:=7; % height in units u hoehe:=he*u; % height breite:=wi*u; %for i=0 upto he: %draw (0, i*u)--(breite, i*u) withcolor .7white; %endfor %for j=0 upto wi: %draw (j*u, 0)--(j*u, hoehe) withcolor .7white; %endfor; path p,q; p:=(6u,0.5u)--(6u,0)--(0,0)--(0,6u)--(6u,6u)--(6u,5.5u); pickup pensquare scaled 20; draw p; h=length(p); numeric c,d,detail; color a,b,co; a:=(1,0.45,0); b:=.2white; co:=.2white; detail:=500; for i=1 upto (detail/2): q := subpath (h*(i-1)/detail,h*i/detail) of p; draw q withcolor (2*i/detail)[a,b]; endfor; for i=detail downto (1+detail/2): q := subpath (h*(i-1)/detail,h*i/detail) of p; draw q withcolor (2*(detail-i)/detail)[a,b]; endfor; label ("B",(1/15)*(2.6u,2.7u)) scaled 15 withcolor co ; label ("2" infont defaultfont scaled 8,(4.9u,4.1u)) withcolor co; % end copy here for online previewer endfig; end;

Here’s another example of a figure where I needed a grid of disks aligned on top of another figure. Using loops in Metapost allowed me to get what I wanted:

For the high quality pdf click here: cioranescu-murat-0 and see the code below

prologues:=3; verbatimtex %&latex \documentclass{minimal} \begin{document} etex beginfig(0); u:=25; % 25 = 25bp = 25 PostScript points = 25/72 in wi:=10; % width in units u he:=7; % height in units u hoehe:=he*u; % height breite:=wi*u; path p,pa; p:=(0,4u)..(0,2u)..(2u,2u)..(3u,u)..(3u,0)..(6u,u)..(7u,3u)..(9u,5u)..cycle; pa:=(3u,5u)..(5u,6u)..(8u,6u)..(8u,3.8u)..(7.5u,3.4u)..(7u,3u)..cycle; draw p; fill p withcolor .8white; pair a,b; h=length(p); i=5; numeric c,d; c:=0.65*h; d:=0.7*h; a:= point c of p; b:= point d of p; pickup pencircle scaled 1.5; %draw a; %draw b; path q,r,s; q:= subpath (c,d) of p; %draw q withcolor red; %r:= b..((a+b)/2 shifted (.5(a-b) rotated -90))..a; %draw r; %fill buildcycle(r,q) withcolor .5red; %fill pa withcolor .9999blue; %label.rt(btex $\Omega$ etex,.5(u,3u)) scaled 2; draw (-u,9u)-- (10u,9u)--(10u,-2u)--(-u,-2u)--cycle; for i=0 upto 9: for j=-1 upto 8: draw fullcircle scaled 0.3u shifted (i*u, j*u); fill fullcircle scaled 0.3u shifted (i*u, j*u) withcolor white; endfor endfor; endfig; end;

There are many ways today to draw what you want. Metapost is one of the tools available, if you like coding.

### Beni Bogoşel

### Blog Stats

- 236,964 hits

### Blogroll

- Annoying Precision
- Beni Bogosel – Research Website
- Blame it on the Analyst
- Bozhidar Velichkov
- Cezar Lupu
- Computational Science
- Cut The Knot
- Dorin Bucur
- Edouard Oudet
- Eppstein Math Fun
- LAMA Chambery
- Math Fun Facts
- Math Prelims
- Math Reference
- Mathematical Ramblings
- Mathlinks
- Ngô Quốc Anh
- Open Problem Garden
- Portuguese Math blog