A few words on fonts

Timothy Murphy

                 (tim@maths.tcd.ie)

20 June 1994

Abstract

The New Font Selection Scheme (NFSS) is now an official part of L^{A}TEX. The concept is beautifully executed but — in this author’s eyes — poorly documented. This brief note is intended as a simple introduction to NFSS.^{1}

1 The 5 parameters

A font F in L^{A}TEX is specified by 5 parameters: encoding, family, shape, series and size. In mathematical terminology, the font F is a function of 5 arguments,

F (e, f, s, s′, s′′),

where e is the encoding, f the family, s the shape, s′ the series and s′′ the size.

The aim of the font selection mechanism is to associate to each such specification a real or virtual font at a particular magnification. For example, if the 12pt option is given at the start of the document, eg

  \documentclass[12pt,a4paper]{article}

then the default font is

F (OT1, cmr, m, n, 12) ↦ cmr12.

(Where no magnification is specified, as here, it is understood that the font is unmagnified.)

1Any comments, corrections or criticisms gratefully received.

In other words, the font selection scheme sets up a mapping

F : encoding × family × shape × series × size → font × magnification.

This mapping is usually defined in Font Definition (or .fd) files, as we shall see shortly.

Each of the 5 parameters can be changed independently of the others by

the commands: \fontencoding, \fontfamily, \fontshape, \fontseries

and \fontsize, followed by an argument and \selectfont. (A change only comes into effect when the command \selectfont is given; this allows more than 1 of the parameters to be changed at the same time.) For example, the command

  \fontshape{cmss}\fontseries{bx}\fontsize{12}\selectfont

will change the font to

                F (OT1, cmss, bx, n, 12) ↦ cmssbx10.

2 Short cuts

In practice these commands \fontencoding, etc, rarely if ever appear in L^{A}TEX documents, because L^{A}TEX offers a number of simplifications.

For example, one would normally write \textss{...} rather than

\fontfamily{cmss}\selectfont.

Similarly the only shape change encountered in most documents is effected by \emph{...}, which changes the shape from ‘roman’ (upright) to italic, or italic to roman, according to the context. (Actually, most of these commands do a little more than just change the font — for example \emph also checks that there is appropriate spacing when coming in or out of the font change.)

Again, the only series change commonly met is \textbf{...}, which is

(almost) equivalent to \fontseries{bx}\selectfont.

Font sizes are rarely expressed in absolute terms; rather, the commands

\small, \large, \Large, \LARGE, \huge, \Huge are used. The actual font

sizes defined by these commands depend on the default font size. For example, if the 12pt option was specified at the start of the document then \large is 14.4 points.

3 Font definition files

For convenience, the definition of the font selection mapping F is normally ‘hived off’ into separate Font Definition (.fd) files.

When L^{A}TEX encounters a change of family it looks to see if there exists an appropriate .fd file, and if there is, it reads it in. The name of the .fd file is formed by concatenating the encoding and the family name, and adding the extension .fd. For example if the family is cmss, say, and the encoding is OT1 (Knuth’s original 128-character encoding) then the file is called OT1cmss.fd. If the family is cmr and the encoding is T1 (the new 256-character Cork encoding) L^{A}TEX will look for the file T1cmr.fd, which specifies DC fonts, eg

F (T1, cmr, m, n, 12) ↦ dcr12.

Thus the effect of specifying T1 encoding is to replace the usual CM fonts by their DC counterparts. The easiest way to do this is to use the t1enc package:

  \documentclass[12pt]{article}
  \usepackage{t1enc}

No further change is needed.

(L^{A}TEX remembers which .fd files it has read in, and will not read them in a second time. In particular a number of standard .fd files such as OT1cmr.fd have already been read into the format file latex.fmt and so are always available.)

4 Introducing a new family

Now suppose we want to introduce a new font. In principal one can introduce a single font at a single size by a command like

  \font\quengwar = tengwar scaled \magstep2
and then write eg {\quengwar \^e l\^u\.r\'i \^l\^DR}. But it is almost

as easy, and much more versatile, to introduce a new family, Tengwar say, with the command

  \newcommand{\quengwar}{\fontfamily{Tengwar}\selectfont}
Now when L^{A}TEX reads {\quengwar \^e l\^u\.r\'i \^l\^DR}, if this is its

first encounter with Tengwar, it will read in the file OT1Tengwar.fd.

We are assuming here that tengwar is in the old (but still standard) Knuth OT1 encoding, as will be the case unless the contrary is specified. If we had wanted to assign it the ‘unknown’ encoding U we would have said

\newcommand{\quengwar}{\fontencoding{U}\fontfamily{Tengwar}\selectfont}

and L^{A}TEX would have looked for the file UTengwar.fd.

But to tell the truth, the encoding does not make any practical difference; the only difference lies in the contents of the corresponding .fd files.

5 Inside a font definition file

Let’s suppose first that we have created the file tengwar.tfm from the Metafont file tengwar.mf, eg by the command

  mf "\mode=nullmode; input tengwar"

Then the only Tengwar fonts at our disposal are tengwar at various magnifications.

The following file OT1Tengwar.fd will serve our purpose

  \DeclareFontFamily{OT1}{Tengwar}{}
  \DeclareFontShape{OT1}{Tengwar}{m}{n}{ <-> tengwar }{}
  \endinput

The first line here introduces the family name Tengwar; while the second specifies that if Tengwar is encountered anywhere in normal text then the font tengwar is to be used at the appropriate magnification. For example, if the document is being printed in 12pt then the font tengwar scaled 1200 will be used.

(If we are using dvips — and the same of true of some other drivers — we do not need to create any pk files; they will be created automagically as and when required.)

But suppose some Tengwar appears in the title of the document. The title is printed in bold type. Since we have not specified any bold Tengwar font, the rules of L^{A}TEX prescribe that the default font family (normally Computer Modern) should be used, in bold at the appropriate size.

This is almost certainly not what we wanted. We can specify that we still want to use our single tengwar font in this situation by adding the following ‘substitution rule’ to the .fd file (before the \endinput!):

\DeclareFontShape{OT1}{Tengwar}{bx}{n}{ <-> ssub * Tengwar/m/n }{}

This specifies that the non-bold Tengwar font should be substituted whenever bold Tengwar is requested. Other substitution rules should be added if necessary, eg if Tengwar is likely to encountered in italic (or emphasized) text.

It is not possible, unfortunately, to say that whenever Tengwar is encountered the tengwar font is to be used. The various substitutions have to be specified individually.

6 Another example

We only had 1 Tengwar font, in a single size. Often of course a font is provided in several sizes. Consider for example Knuth’s concrete fonts. The distribution contains the following .mf files: ccr5.mf, ccr6.mf, ccr7.mf, ccr8.mf,

ccr9.mf, ccr10.mf, cccsc10.mf, ccmi10.mf, ccsl10.mf, ccslc9.mf, ccti10.mf.

We might start OT1concrete.fd with the lines

  \DeclareFontFamily{OT1}{concrete}{}
  \DeclareFontShape{OT1}{concrete}{m}{n}
    { <5> <6> <7> <8> <9> sgen * ccr <10-> s * ccr10 }{}

This specifies that if the font is requested say at size 7 then the file ccr7 is to be used, while for any size ≥ 10 the file ccr10 is to be used, suitably magnified. (The ‘s’ in sgen and s * stands for silent — meaning that we do not want to be told about font changes.)

There doesn’t appear to be a bold font included, so we might want to substitute the above fonts in this case, by

  \DeclareFontShape{OT1}{concrete}{bx}{n} { ssub * concrete/m/n }{}

We can use ccti10 for text italic, in all sizes:

  \DeclareFontShape{OT1}{concrete}{m}{it} { <-> ccti10 }

As for substitutions, the simplest plan is to look at the file OT1cmr.fd and consider what combinations of shape and series are likely to arise.

It remains to see how we can use the fonts. If we want to mix concrete and CM we could introduce the command

  \newcommand{\concrete}{\fontfamily{concrete}\selectfont}

and then we could say, for example,

  The {\concrete concrete} fonts ...

On the other hand, if we want to replace cmr throughout by ccr we can say

  \renewcommand{\familydefault}{concrete}

in the preamble to our document, and the whole document will be printed using the concrete fonts.

Am I too loud?

One last small point. You might consider the Tengwar font (say) too large in comparison with normal text. NFSS offers a simple solution. If we change the line in the OT1Tengwar.fd file to read, for example,

  \DeclareFontShape{OT1}{Tengwar}{m}{n}{ <-> s * [0.95] tengwar }{}

then the font will be reduced in size by 5%.

Mathematical fonts

In this document we have only considered text fonts. The use of fonts in mathematics (whether displayed or not) is another story, for another day ….