MetaPost for Anthropologists

Leigh-Anne Mathieson 19 Aug 2009

Going from MetaPost File to Diagram

You are going to need the MetaPost interpreter, as well as the usual TeX stuff to produce your figures. There will be two important files for you to be concerned with. The first is the MetaPost file which contains the directions for drawing your figure. For the purposes of this tutorial, let's say we've called this file foo.mp. When you use the MetaPost interpreter to run this file, if you've made two figures they will be output as two .eps files, called foo.1 and foo.2. The next important file is a TeX file that will display the figures for you. In this tutorial, let's call this TeX file display.tex. If your MetaPost file has produced two figures, then your TeX file is going to look something like this:

\documentclass[letterpaper,10pt]{article}
\usepackage{graphics}
\begin{document}

\begin{figure}
\centering
\includegraphics{foo.1}
\end{figure}

\begin{figure}
\centering
\includegraphics{foo.2}
\end{figure}

\end{document}

If you've heard of a utility called pdflatex, you can't use it here because pdflatex can't handle .eps files directly.

On OS X or GNU/Linux

If you run Linux, you probably already know how to make your MetaPost and TeX files. If you run OS X, you can just make your files in TextEdit, but you won't get syntax highlighting. Personally, I prefer Macvim. TeXShop comes with the MacTeX-2008 installation and it has syntax highlighting for .tex and .mp files.

You will need to have a TeX distribution installed. TeXLive should do nicely. You will need to place your MetaPost file, and your TeX file in a directory. Once you have a MetaPost file, you need to use the MetaPost interpreter to get your figures. First, you need to open a terminal and navigate to the directory where you stored your files (recall: use cd directory1 to navigate to directory1 from the current directory, and ls to display the contents of the current directory).

Now, you can run mpost foo.mp. Now you should have your figures sitting in your directory and all that's left is to display them. What you need to do next is run latex display.tex. This will output a bunch of files but we're only concerned with display.dvi, which we're going to convert to a .pdf like so: dvipdf display.dvi . Voila! You should now have a file display.pdf and you can look at your figures. Since you'll likely need to run these commands more than once, it's handy to run them in a batch like this:

mpost foo.mp && latex display.tex && dvipdf display.dvi &

On Windows

To make your files, you can just use Notepad if you like, but you won't get syntax highlighting. Personally, I prefer Vim.

If you don't have a TeX distribution installed already, installing MiKTeX will give you everything you need. Once you have your TeX distribution installed, the first thing you need to do is open up a command prompt and navigate to the directory you stored your files in (recall: use cd directory1 to navigate from the current directory to directory1, and dir to display the contents of the current directory). Now, you can run the MetaPost file to get your figures like so: mpost foo.mp.

Now that you have your figures waiting in your directory, all you need to do now is use your LaTeX file to display them. Start by running latex display.tex. Now, you should have a .dvi file called display.dvi. The machine I tested on didn't have a PostScipt viewer, so I was looking to see my results in a .pdf. However, the pdf produced by dvipdfm didn't actually contain the figures so I had to convert the .dvi to a .ps, then convert the .ps to a .pdf like this: dvips display.dvi && pstopdf display.pdf &.

Since you'll likely need to run these commands more than once, you might want to run them in a batch like this instead:

mpost foo.mp && latex display.tex && dvips display.dvi && pstopdf display.ps &


Why does this tutorial even exist?

This all started when I was trying to think of skills that I, as an undergraduate computer scientist, could trade with my good friend Joey, a graduate anthropologist. As I was working on MetaPost diagrams at the time, I jokingly said, "Well, I could teach you how to make your diagrams in MetaPost instead of Microsoft Paint. It would be a pain to work with, but hey, you'd be able to scale your diagrams up and down as much as you like."

Joey said, "Hey, you know, that would actually be useful," so we met up on a sunny afternoon and got MetaPosting. Joey determined that this wasn't as hard as I'd made it sound, and then I determined I'd just been whining all this time.

What kind of diagrams will this tutorial teach me to make?

Simply put, diagrams with boxes (or circles) and arrows. After making your way through this tutorial you should be able to make something like this:


In this section, we are going to walk through how to make the .mp file for the diagram above. You can download the .mp file used for the diagram here and the .tex file here.

The first thing we need to do is take the draft of your diagram, and place each box on a grid:

Now, take a look at the MetaPost file you downloaded. You are going to modify the code between beginfig(1) and endfig; . The variable h is your x scale, and the variable w is your y scale. You probably don't need to touch these unless your figure explodes off the page. Now, you'll need to specify what goes in each of your boxes. For example, boxit.cu (btex \bf Culture \rm etex); sets the culture box. You'll need to refer to this box again later as "cu" but you can call it whatever you like. You'll need a boxit line with the label in the brackets for each box.

Aside: bold, italic, and multi-line labels
To make bolded text, put the tag \bf in front of the section you wish to bold, and \rm afterward. For italics, use \it instead of \bf. If your label needs more than one line, you will need to use \hbox and \vbox. The whole label needs to be in an \vbox like this: \vbox{--all of the label text goes here--} and each line is placed in an \hbox like this \hbox{--one line of the label goes here--}. All of the lines are then placed in the \vbox. So, the general format for this is \vbox{ \hbox{line 1 text here} \hbox{line 2 text here} }.

Now that you've given your boxes labels, you need to define pairs for where you are going to place your boxes on the grid. You need a line that looks like cu.c=(0h,2w); for each box. Don't forget that you need to multiply your x and y coordinates by their scales (h and w). Don't forget to add the ".c" because since you are defining a pair, it needs a distinct name.

The forsuffixes loop colours the background of the box with white so that it won't be transparent. You need to change the suffixes to match the short names you gave your boxes at the start.

The line drawboxed (cu,ap,be,sy); is what actually makes the boxes appear in the diagram, so make sure that all of the short names of your boxes are in the brackets.

Arrows

To make arrows with straight lines, you will need to use drawarrow . In the line drawarrow cu.c--ap.c cutbefore bpath cu cutafter bpath ap;, cu.c is the starting location, and ap.c is the end location. drawarrow will stick the arrow pointing to the end location. To make sure that the arrows don't overlap with the boxes, you need to add cutbefore bpath {short name of box the arrow originates from} cutafter bpath {short name of box the arrow points to} .

drawarrow cu.c{dir-50}..{dir 50}sy.c cutbefore bpath cu cutafter bpath sy; draws the curved arrow from "Culture" to "Syncretism". There are two things that change with curved arrows. First, you will need to connect your pairs with .. instead of -- so that MetaPost knows to allow curved arrows. The other change is the added direction. In the line drawarrow cu.c{dir-50}..{dir 50}sy.c cutbefore bpath cu cutafter bpath sy; , dir -50 tells MetaPost to start drawing the curve at -50 degrees. The dir 50 tells MetaPost that the arc should come into the other box at 50 degrees. Remember that you can always adjust this angle later.

Closing Remarks

If your figure doesn't immediately come out exactly as you dreamed, do not despair! You'll have to expect that you may need to change some angles, or placement of your boxes.

I assumed you were drawing boxes, but if you want to draw circles instead, the only thing you need to change is to use circleit instead of boxit.

If you have questions, you're welcome to email me at f3s6 at ugrad dot cs dot ubc dot ca.