Getting Started With Metapost And Metafont: An Introduction

What is Metapost and Metafont?

Metapost and Metafont are software systems for producing digital typography and vector graphics. Developed by Donald Knuth in the 1970s and 80s, they play a key role in automating the process of typesetting and drafting geometric images as part of document preparation.

The Metafont system provides tools for designing vector font formats. It outputs font files that can be used by digital typesetting systems. Metapost builds on Metafont by adding capabilities for drawing more general graphics and illustrations in a similar vector format.

Together, Metapost and Metafont provide an integrated solution for high-quality and reusable digital typography and drawings. The output can be included directly in electronic documents, Sent to professional printing devices, and integrated into workflows involving TeX and LaTeX typesetting.

Unlike bitmap graphics and proprietary font technologies, Vector images and fonts from Metapost and Metafont provide excellent scaling, rotation, and fidelity when printed from different devices at any resolution. The systems focus on robustness and flexibility rather than simple WYSIWYG editing.

Installing Metapost and Metafont

Metapost and Metafont are available free of charge and included in major TeX/LaTeX distributions like TeX Live. To install them, you will need to download and run the installer for your operating system.

Linux

Most Linux package managers have available packages for a full TexLive distribution containing Metapost and Metafont. On Debian/Ubuntu systems, run:

sudo apt install texlive-full

This will download texlive and all associated packages, including Metapost and Metafont. The full installation requires several gigabytes of disk space.

Windows

Go to the TexLive website and Download the TexLive installer for Windows. Run the installer and select "Install TexLive" to install the complete distribution, including all packages.

The process may require 10-15 minutes to download and install all the components.

Mac OS

Like Windows, go to the TexLive site and use the MacTeX distribution which contains TexLive bundled for MacOS. Download the installer DMG file and run it to install MacTex.

Alternatively, if you have Homebrew package Manager installed, you can use:

brew install mactex 

To automatically download and install MacTex.

Verifying the Installation

To confirm Metapost and Metafont are correctly installed and in your path, open a command prompt/terminal and check the versions:

metapost -version
mf -\version

This should output the version information if they are successfully installed.

Troubleshooting Tips

If you have trouble running Metapost commands after installing TexLive packages, you may need to update system paths to add the binary directories.

On Linux/Mac, ensure /usr/local/texlive/YYYY/bin/arch is in your PATH. On Windows add the path to \texlive\YYYY\bin\win32 instead.

YYYY refers to the TexLive release year, usually the current year. Arch and win32 refer to system architecture.

Anatomy of a Metapost Document

The structure of a basic Metapost graphics file has a few key components:

Preamble

Start by importing any packages/modules needed with input statements. Then set up variables, external files, and settings:

input colorbrewer;
input graphs; 

numeric x_step;  x_step = 10;
numeric y_step;  y_step = 15;

verbatimtex 
\documentclass{article}
etex;

Drawing Commands

Use Metapost primitives like box, circle, fill, draw to construct geometric objects. Specify coordinates, dimensions, colors, etc.

draw (.5x_step, 2y_step) -- (3x_step, 1cm); 
fill fullcircle scaled 30; 
boxit.top(btex this is text etex) enlarged 5pt;

Output

At the end, output graphics in desired format like PDF, encapsulated PostScript.

outputtemplate := "%j-%c.mps";
beginfig(1);
  ... drawing commands here
endfig;
end.

Drawing Shapes and Paths

Metapost includes a set of primitive graphics operations for compositing vector shapes, as well as many predefined shapes.

Lines and Polylines

The -- operator draws a straight line between two coordinates:

draw (0,0) -- (100, 100);

Combine -- with .. controls to create Bezier curves:

draw (0,0) .. controls (50, 200) and (200, 50) .. (300, 300); 

Rectangles, Ovals and Circles

Metapost provides shortcuts for common shapes:

draw unitsquare xscaled 100 yscaled 50; % rectangle

fill fullcircle scaled 20; % filled circle
draw halfcircle scaled 20; % half/semicircle 

fill buildcycle(ellipse scaled 200, 100); % ellipse

Transforms

Use operators like rotated, slanted, scaled to transform graphics:

fill fullcircle xscaled 30 yscaled 20 rotated 30;

Combining Paths

Set operations like intersection and union merge paths:

path circle; circle = fullcircle scaled 20;
path box; box = unitsquare scaled 50; 

fill circle intersectionbox box; % intersected section
draw circle union box; % outline of merged paths

Adding Text Labels

Text handling in Metapost builds on TeX typography. The btex/etex operators Set inline text formatting and alignment.

Text Formatting

Apply text styles and typeface:

label(btex \bf\it This is Bold-Italic text etex, (0,0));
label(btex \font\times=ptmr8t at 10pt Hello etex, (0,0));

Positioning Text

Many methods to position text relative to graphics:

label(btex (50,100) etex, (50,100)); % explicit point
label(btex This is centered etex, center fullcircle scaled 100); 

boxit.top(btex top-aligned etex) withcolor red;
boxit.lft(btex left-aligned etex) withcolor blue; 

Aligning Text

Align text interactively using boxit macro:

boxit(btex this is top-left etex) withcolor red; 
boxit.top(btex this is top-right etex) withcolor green;

Producing Output

When your Metapost graphics code is complete, produce final output in PostScript, PDF format for inclusion in documents.

Formats

Set output file template and generate graphics:

outputtemplate := "%j-%c.pdf";  

beginfig(1);
  % drawing commands
endfig;

For EPS encapsulated PostScript, change template to .eps extension.

Inclusion in LaTeX

Use epsfig package to embed:

\documentclass{article}
\usepackage{epsfig}
\begin{document}

\includegraphics{fig1-1} 

\end{document}

Print and Production

Vector EPS and PDF graphics scale to high resolutions without pixelation making them ideal for production use.

For professional publishing, follow industry best practices for color spaces and bleed allowances.

Next Steps with Metapost

Learning More

Metapost documentation tends towards the technical side but many additional examples and tutorials exist:

  • The Metafont Book - Knuth's definitive manual
  • Making TeX Work - contains Metapost tutorial
  • Metapost Wikibooks page

Integration

Consider integrating Metapost graphics into wider publishing workflows:

  • TeX, LaTeX, ConTeX - typesetting engines
  • Math typesetting - equations, functions, etc
  • TIKZ, PGF - alternate graphics languages

Their data and parameterization abilities enable large-scale automated document generation.

Complex Graphics

Many extensions available for advanced graphics features:

  • OzMP graphics engine
  • MetaUML for diagrams
  • MetaPost Bezier Library

Significant practice is needed to leverage Metapost's power and flexibility for production tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *