Getting Started
Prerequisites
We will need a few things before we can start using MetaPost:
-
A terminal of some sort. I will be using Powershell on Windows but really any shell/terminal/command prompt should be fine.
-
A plain text editor, like notepad or emacs.
-
A place to work. MetaPost will generate a few files, it is easier to see whats going on if you start your work in an empty folder.
Installing MetaPost
MetaPost is often used in conjunction with LaTeX and many TeX distributions include metapost. The simplest way to get MetaPost installed on your computer is to install one of the widely available TeX distributions that include it. Here we will use MiKTeX:
https://miktex.org/howto/install-miktex
Once you have MiKTeX installed you should be able to open your command prompt or terminal and issue the command mpost
or mpost.exe
on Windows.
You should see a response like this:
You are looking at the MetaPost compiler, waiting for you to feed it some MetaPost code. Normally we will be passing a source file location along when calling the compiler, but you could type it directly in here.
You can press ctrl-X
to exit the metapost compiler.
As you can see, we will be using MetaPost 2.0 for this course, if you have an older version some things might not work as expected so please check that your version number is at least 2.0.
Hello world
Now that MetaPost is installed lets create our first figure.
Create a text file, lets call it "HelloWorld.mp". We typically use the .mp extension for MetaPost files. Copy the text below into you file, we will be going through it line by line.
outputformat := "png";
beginfig(1)
label("Hello World",origin);
endfig
end
By default MetaPost produces encapsulated post-script files, this format is great for including images in a larger file when you are working with LaTeX, but since we will be using MetaPost stand-alone for this course we will prefer to produce PNG output. To instruct MetaPost to output PNG our first command will be:
outputformat := "png"
This is an example of setting an internal variable, 'outputformat' is a variable that we will not be using in our code, but rather it is used to configure the MetaPost interpreter. You can also set it to "svg" to generate svg output.
To begin a figure we write
beginfig(1)
The 1 is a number for our figure, by default MetaPost will include this number as the file extension of the figure output. To write some text on our figure we can use a label.
label("Hello World",origin);
as you can probably guess this places the text "Hello World" at origin (i.e. at the (0,0) coordinate). Now we end the figure, and since we don't want to include more figures in this file we end the file as well with end
endfig
end
generally lines are terminated with ;, but its not necessary for ending the last figure and file. Save the file and return to your terminal. If MetaPost is correctly installed you should be able to issue the command.
mpost.exe HelloWorld.mp
Hopefully you will see an output similar to this:
Congratulations, you just produced the file "HelloWorld.1". This will be your figure, open it op and have a look. You might have to rename it to .png to make it recognizable as a PNG image, we will look into this in the exercises.
You will notice that the text we wrote is very small, again we will improve this in the exercises.
Exercises
outputtemplate
is another internal variable. It controls the filenames (including file extensions) of MetaPost output.
By default outputtemplate
is set to "%j.%c".
Here %j
refers to the input filename and %c
to the figure number.
hppp
and vppp
are another two internal variables, they control the resolution of the png output by setting horizontal/vertical points pr. pixel which defaults to 1.
Having fewer points pr. pixel make images larger.
Experiment with setting these three variables to produce a png output with the .png file extension and a higher resolution than "HelloWorld.1".
You can also experiment with producing svg and postscript output.
Hint
outputformat := "png";
outputtemplate := "%j_%c.png";
hppp := 0.25;
vppp := 0.25;