Chapter 11. Introducing MetaFont
TeX builds pages out of boxes and glue. The smallest boxes are usually individual characters. MetaFont is a tool that creates the actual characters.
In the last few years, a real explosion has taken place in the number of readily available fonts. Today, many people have access to a large number of high quality typefaces. Not too long ago (before Adobe Type Manager (ATM) was available for the Macintosh and Microsoft Windows), fonts were too expensive to be generally available. In those days, a font building tool like MetaFont was essential if TeX was going to have a number of typefaces and a large number of mathematical symbols. Today, the role of MetaFont is diminishing. Many people choose to use PostScript fonts almost entirely. In fact, with PostScript alternatives to the Computer Modern Math fonts now available, it's possible to use TeX without using MetaFont fonts at all.
On the other hand, lots of people do still use the Computer Modern fonts, and there are many other MetaFont fonts (for non-English languages like Arabic and Japanese, for example), so MetaFont is still a useful tool. Also, it can be used to make nice portable diagrams.
This chapter cannot teach you how to harness all of MetaFont's power. The only practical way to learn how to design your own images with MetaFont is to read The \MF{}book [kn:mfbook] (even if your images are nowhere near as complex as an entire font).
What you will learn from this chapter is how to run MetaFont to create TeX PK fonts and TFM files from existing MetaFont programs. The Computer Modern fonts, the \AmS fonts, and the DC Fonts are all created with MetaFont. It is not unreasonable to imagine that you might someday want to create one of these fonts at a non-standard size or unusual magnification.
Knowing how to run MetaFont will also help you set up a system for performing automatic font generation. This can save a lot of disk space. See the section called “the section called “Automatic Font Generation by DVI Drivers”” in Chapter 5, Chapter 5, for more information about setting up automatic font generation.
From many perspectives, TeX and MetaFont behave in analogous ways. Where TeX reads a plain text TEX document, processes it, and produces a DVI file, MetaFont reads a plain text MF program, processes it, and produces a generic bitmap font file as output.
Because you probably aren't going to be writing your own font programs for MetaFont, you have to get them from somewhere. Many distributions of TeX include MetaFont and the font files for the Computer Modern fonts. This chapter will concentrate on just the Computer Modern font programs because those are the most likely to be available.
Many MetaFont fonts can be retrieved from the CTAN archives. Table 11.1 lists some common fonts and their location in the CTAN archives. A collection of font samples is shown in Appendix B, Appendix B.
Table 11.1. Some Popular MetaFont Fonts on the CTAN Archives
Fonts | Location |
Standard Computer Modern | fonts/cm |
Standard LaTeX | macros/latex/distribs/base/fonts |
AmS Fonts | fonts/ams |
DC Fonts | fonts/dc |
Concrete | fonts/concrete |
Pandora | fonts/pandora |
Ralph Smith's Script Font | fonts/rsfs |
St. Mary's Road | fonts/stmary |
What to Run?
Like TeX itself, the actual MetaFont file you have to execute varies between platforms and implementations. If you have built and/or installed TeX, you probably already know what program to run. You'll have to ask your system administrator for help if you cannot figure out what the name of the MetaFont executable is on your computer. In the rest \linebreak
of this chapter, I'll assume that the command mf runs MetaFont. You should substitute the name of the executable program on your system for mf in the examples that follow.
MetaFont comes in big and small versions just like TeX. You will need the big MetaFont if you are building fonts for very high-resolution devices or at very large sizes. Most fonts can be built with a small MetaFont.
What Files Does MetaFont Need?
In addition to the MetaFont programs, MetaFont must be able to find several other files. The files that are needed are normally created during the installation process. The sections that follow describe each of the files that MetaFont needs.
Pool Files
The pool file for each version of MetaFont has the same purpose as the pool file created for TeX. It is created when MetaFont is compiled. If you don't have a pool file, there's nothing you can do about it. If you obtained precompiled programs (from the Internet, from a friend, or commercially) and you don't have the pool file needed by MetaFont, you received an incomplete distribution.
If you did not install MetaFont, and the pool file is missing, contact the system administrator who performed the installation. Something was done incorrectly.
Base Files
Base files for MetaFont are analogous to format files for TeX. Unlike TeX, which has many different format files, there are relatively few base files for MetaFont.
Base files are created by a special version of MetaFont, usually called \iniMF. However, some implementations combine MetaFont and \iniMF into one program, in which case you must select \iniMF with a special option when you run the program.
Like iniTeX, \iniMF interprets all the control sequences in a set of base macros and builds the in-memory data structures that the MetaFont program needs. After loading all the files, \iniMF writes the memory image into a base file. When MetaFont loads the base file, it simply copies it into memory; no interpretation is necessary.
Because MetaFont loads the base files without interpretation, they are not generally portable from one system to another, or even between different versions of MetaFont on the same system. Different versions of MetaFont load differently in memory, and this makes the base files incompatible. For example, you need a big \iniMF to make base files for big MetaFont and small \iniMF for a small MetaFont.
The “the section called “What to Run?”” section later in this chapter describes how to build a base file, if you do not already have one.
MetaFont Programs
When you run MetaFont, you have to tell it what file (MetaFont program) to process. MetaFont programs are stored in files with the extension .mf. If you specify a complete pathname, MetaFont will load the specific file that you request. If you specify a simple filename without a path, MetaFont looks for the file in several user-defined and, possibly, system-defined locations. The most common way to specify user-defined locations is by setting the MFINPUTS environment variable to a list of subdirectories where MetaFont programs are kept. The format of the environment variable differs according to the platform you use. On unix systems, it is a list of directory names separated by colons. On MS-DOS and OS/2 systems, it is a list of directory names separated by semicolons. Consult the documentation for the particular implementation of MetaFont you use for more information about system-defined locations where MetaFont looks for input files.
Command-line Options
In addition to the name of the MetaFont program, MetaFont has very few command-line options. The name of a base file is the only option regularly used. In this section, I'll explain what can go on the MetaFont command line.
A formal specification of the MetaFont command line looks like this:
$ mf switches &base mf-program mf-commands
After the name of the MetaFont program, the first things that you can specify on the command line are implementation-dependent switches and options. For example, implementations of MetaFont that combine \iniMF and MetaFont into a single program may use the switch /I to specify that \iniMF processing is desired. There are no system-independent switches for MetaFont. Consult the documentation that comes with your implementation for more information about system-dependent switches.
After any system-dependent switches, you can specify the name of the base file to use. This option, if present, must come before any other options, and you must put an ampersand (&) in front of the base file name. If you do not specify a base file, MetaFont will use a default base.
After the base, MetaFont looks for the name of a MetaFont program file. If MetaFont finds a filename on the command line, it will process the program contained in that file before looking at any other options that may follow.
Finally, you can insert arbitrary MetaFont commands on the command line by typing them just as you would in a program. In fact, this is frequently done to change the size or magnification of the font being created. Exactly how this is accomplished is described in the section called “the section called “Running MetaFont”” later in this chapter.
Please read the section called “the section called “Command-line Options”” in Chapter 3, Chapter 3, for a detailed description of some common problems with MetaFont commands on the command line. They are exactly the same problems that can occur on the TeX command line.
Also, the behavior of MetaFont when run without any options at all is the same as the behavior of TeX as described in the section called “the section called “Filenames and TeX”” in Chapter 3.
Building a Base File
Building a base file for MetaFont is exactly analogous to building a format file for TeX. Fortunately, there are far fewer base files. The two most common base files are the plain base and the cmbase (the base used for the Computer Modern fonts). Use the \iniMF program to build a base file. For example, to build the plain base, use:
\$ inimf plain
Move the resulting files, plain.base and plain.log, into the directory where MetaFont looks for base files (typically in a bases or formats subdirectory under the standard TeX directory).
The cmbase is actually an extension of the plain base. To build it, use the command:
\$ inimf &plain cmbase
Remember to use appropriate quotation or shell escape characters to prevent the ampersand from being misinterpreted.
Running MetaFont
The sections that follow describe how to use MetaFont to make a font from an existing MF file. You should reread the sections “the section called “What TeX Needs To Know”” from Chapter 5, Chapter 5, and “the section called “Built-in Fonts”” from Chapter 8, Chapter 8, if you are unfamiliar with the notions of bitmap and device resolution and magnification and design size in TeX fonts.
Picking a Mode
The input to MetaFont is an “outline” description of a font. The output is a set of bitmaps that realize the outline on a particular device at a particular size.
Output from MetaFont is naturally device-dependent because different printers have different resolutions. But even two printers with the same resolution sometimes produce different looking results from the same bit patterns. For example, some laser printers are “write white” printers, and some are “write black” printers---depending on whether the printer fills in the black (text) areas or the white (non-text) areas of the page. A small, intricate bitmap (like a character from a font) designed for a write-black printer may not look as good on a write-white printer and vice versa.
There are several internal parameters that can be modified to produce optimal output on any given device. These parameters are grouped together in a mode. Whenever you run MetaFont to create a new set of bitmaps, you must select a mode.
There are no rules for determining what parameters produce the best mode for a particular printer; it's just done by trial and error (set some values, print a font, see if it looks good, adjust some values, print another font, etc.).
Luckily, a large collection of predefined MetaFont modes is available in the CTAN archives. The file fonts/modes/modes.mf contains appropriate MetaFont modes for many printers.
Each mode has a name. For example, laserwriter and LNOthree are two modes from modes.mf. See “the section called “Running MetaFont”,” later in this chapter for how to select a mode when you run MetaFont.
Internally, MetaFont has a variable called mode that it uses to keep track of the current mode. The examples in this chapter use the laserwriter mode because that is appropriate for my printer.
Proofing mode
Proofing mode is the mode that MetaFont uses if you do not select another mode. In proofing mode, MetaFont displays each character on the screen. This mode does not usually produce a useful font because it sets the device resolution to 2601.72 dots per inch (dpi). That resolution is chosen so that there are exactly 36 pixels per point (one point is 1/72.27 inches in TeX).
Because font designers are going to want to look at a font on the screen far more often than they will want to print it, MetaFont uses proofing mode as the default.
If you find that MetaFont is producing fonts with huge resolutions (thousands of dots per inch) or fonts without TFM files, you're probably running in proofing mode. Remember to use the mode-setting commands on the command line when you run MetaFont. These commands are described in “the section called “Running MetaFont”,” later in this chapter.
Selecting a Size
The design size of the font you are creating is determined by the MF file you use. For example, the design size of cmr10.mf is 10pt. The design size of cmr12.mf is 12pt, etc. To select how large you want the bitmaps to be, you must set the magnification. There is no other way to select the size when you are running MetaFont.
If you know the design size of a font and the size of the bitmaps that you actually want to produce, it is easy to calculate the magnification required. The magnification is simply the ratio of the size you want to the design size.
For example, suppose you need a 16pt version of Computer Modern Roman. First, pick the font that has a design size closest to the size you want---in this case, Computer Modern Roman 17pt (cmr17.mf). To calculate the magnification, simply divide 16 by 17; in other words, the desired magnification is 0.9412. To make a 13pt version of cmr12, use a magnification of 1.0833. Remember, in order to use these fonts in TeX, you will have to use the at or scaled operators (for example, \font\cmrxvi=cmr17 at 16pt).
Making a GF Font
After you have selected a mode and a magnification, running MetaFont is easy. In general, you use a command line like the following:
\$ mf {\bs}mode=selected-mode; mag=desired-magnification; {\bs}input mf-file
For example, on a unix system, the following command line would make a GF file for the Computer Modern Roman 12pt font at a size of 13pt:
\$ mf '{\bs}mode=laserwriter; mag=1.0833; {\bs}input cmr12'
Note the use of quotation marks to avoid shell-expansion of the backslash characters.
On an MS-DOS or OS/2 system, where the backslash is not a special character, the 13pt version of cmr12 is made with the following command:
\$ mf {\bs}mode=laserwriter; mag=1.0833; {\bs}input cmr12
As you can see, the semicolons are required in both cases.
Magnification can be expressed with the magstep() function if the font is being built at a standard magstep size. For example, the following command builds a 14.44pt version of cmr12:
\$ mf {\bs}mode=hplaser; mag=magstep(1); {\bs}input cmr12
This corresponds to \magstep1. This example also illustrates how a different mode can be selected. In this case, the font is being built for an HP LaserJet printer. The command is shown without quotations, but they are still necessary on unix systems.
Making a PK Font
Running MetaFont, as shown above, produces a TFM file and a GF file. TeX only needs the TFM file, which can simply be copied to the appropriate directory. For the Computer Modern fonts, you probably already have copies of the TFM files, so the duplicates produced by MetaFont can be deleted. The TFM file does not change at different magnifications, so you do not need to save different TFM files for different sizes.
The GF file contains a font in the “generic bitmap” format. Most DVI drivers use PK files. The GFtoPK program converts GF fonts into PK fonts. If MetaFont creates the file cmr12.gf, you can convert it into a PK file by issuing the command:
\$ gftopk cmr12.gf cmr12.pk
After conversion, you should move the PK file to the appropriate directory for your DVI driver and delete the GF file. PK files are generally stored in one of two directory structures. On operating systems that support long filenames, it is most common to store all the PK files for a given font in the same directory, using the extension to identify the resolution. In this case, a 360dpi version of cmr10 would be stored as cmr10.360pk. Under operating systems like MS-DOS, which do not support long filenames, different resolutions are stored in their own directories. In this case the 360dpi font just described would be stored as cmr10.pk in a directory called 360dpi or dpi360.
What About Errors?
Because I'm operating under the assumption that you are not writing your own MetaFont programs, errors are far less likely. Nevertheless, there are a few things that can go wrong.
Figure 11.1 shows a simple figure eight created with MetaFont. The code which creates this symbol is shown in Example 11.1.
Figure 11.1. A figure eight created with MetaFont
8
Example 11.1. The Code for the Figure Eight
mode_setup; u# := 2mm#; define_pixels(u); beginchar("A", 8u#, 9u#, 5u#); z1 = ( 0u, 0u); z2 = ( 8u, 0u); z3 = ( 1u, 8u); z4 = ( 7u, 8u); pickup pencircle scaled 1u#; draw z4 .. z1 .. z2 .. z3 .. cycle; pickup pencircle scaled 3u#; drawdot z1; drawdot z2; drawdot z3; drawdot z4; endchar; \end
Let's look at some of the things that can go wrong. In the following examples, a unix implementation of MetaFont is being used. Quotation marks are used to prevent misinterpretation of the backslashes.
! I can't find file ‘mode=laserwriter.mf’.
If you forget to put a backslash in front of the mode parameter, MetaFont thinks that mode specification is the name of the MF file. For example, mode=laserwriter is misinterpreted as the filename:
$ mf 'mode=laserwriter; \input eight' This is METAFONT, Version 2.71 (C version 6.1) ! I can't find file ‘mode=laserwriter.mf’. <*> mode=laserwriter
; \input eight Please type another input file name:
! Value is too large (xxxx).
This error occurs if some element of the image goes outside of the bounds allowed by MetaFont. Frequently, this occurs in proofing mode when the image is so large that it can't be rendered.[112] In this example, MetaFont is in proofing mode because I forgot the \mode=laserwriter parameter on the command line:
$ mf '\input eight' This is METAFONT, Version 2.71 (C version 6.1) (eight.mf ! Value is too large (4097). <recently read> ; beginchar->...rdp:=(EXPR3);w:=hround(charwd*hppp); h:=vround(charht*hppp); l.5 beginchar("A", 8u#, 9u#, 5u#) ; ?
This doesn't mean that MetaFont can't render very large images; it just means that my design isn't robust enough to handle an extremely high resolution.
! Curve out of range.
This error is also caused by an image that is too large to be rendered.[113] Again, MetaFont is trying to create the image at an extremely high resolution; it is in proofing mode because I forgot the \mode=laserwriter parameter on the command line:
$ mf '\input eight' This is METAFONT, Version 2.71 (C version 6.1) (eight.mf ! Curve out of range. <to be read again> ; l.11 draw z4 .. z1 .. z2 .. z3 .. cycle; ?
unknown string
This is what happens if you forget the semicolon after the \mode parameter. In this example, MetaFont thinks that “laserwriter \input eight” is the mode, which doesn't make sense:
$ mf '\mode=laserwriter \input eight' This is METAFONT, Version 2.71 (C version 6.1) (eight.mf >> unknown string mode_name0 ! Not a string. <to be read again> ; mode_setup->...nput "&mode)else:mode_name[mode]fi; if.unknown.mag:...
l.1 mode_setup ; ?
! Extra tokens will be flushed.
Oops, another missing semicolon:
$ mf '\mode=laserwriter; mag=magstep(1) \input eight' This is METAFONT, Version 2.71 (C version 6.1) (eight.mf ! Extra tokens will be flushed. <to be read again> warningcheck mode_setup->warningcheck :=0;if.unknown.mode:mode=proof;fi... l.1 mode_setup ?
Nothing happens...
MetaFont is sitting at the asterisk prompt waiting for you to do something. You forgot to \input the font:
$ mf '\mode=laserwriter; eight' This is METAFONT, Version 2.71 (C version 6.1) *
Output written on eight.nnngf...
Success! A 300dpi version of eight.mf has been created and stored in the file eight.300gf:
$ mf '\mode=laserwriter; \input eight' This is METAFONT, Version 2.71 (C version 6.1) (eight.mf [65] ) Font metrics written on eight.tfm. Output written on eight.300gf (1 character, 1964 bytes). Transcript written on eight.log.
On systems that place restrictions on the length of a filename extension, it is abbreviated to eight.300. The next step is to run gftopk eight.300gf. This will produce eight.300pk, which our DVI driver can use.
Other Errors
The list of errors in the preceding sections is hardly exhaustive. For example, the gothic fonts (see Appendix B, Appendix B) produce two more errors when rendered at low resolutions: ! inconsistent equation, caused by apparently contradictory statements (probably due to rounding errors) and {! strange path}, caused by a path that does not appear to run counter-clockwise (again probably caused by rounding errors, these are very complex fonts).[114]
Most of the other errors that are encountered can be ignored (the preceding examples from the gothic fonts can certainly be ignored---at least at 300dpi). Entering H at MetaFont's question mark prompt will describe the cause of the error in more detail.
Output at Very High Resolutions
Producing MetaFont output for very high resolution devices like phototypesetters is sometimes difficult. This will frequently require a big MetaFont for the same reasons that processing a large document requires a big TeX.
It is possible to construct examples that cannot be rendered at very high resolutions because they are simply too large and complex.
Output at Very Low Resolutions
The intricate detail of many characters is difficult to render at very low resolutions. Yet, it is sometimes desirable to produce output at low resolutions for on-screen previewers and dot matrix printers.
Usually, errors like “strange turning path” that result from attempting to produce output at low resolutions can be ignored, but sometimes the resulting characters will be distorted.
[112] {Obtaining this error from eight.mf required changing unit size from 2mm to 5mm.}
[113] {Obtaining this error from eight.mf required changing unit size from 2mm to 4mm.}
[114] {There are very specific rules about which way paths must be drawn so that MetaFont's algorithms for filling in the character's outline can succeed.}
Get Making TeX Work now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.