9.3 Using fonts in text
When you are writing a LaTEX document, appropriate fonts are normally chosen automatically by the (logical) markup tags used to structure the document. For example, the font attributes for a section heading, such as large size and bold weight, are defined by the document class and applied when a \section command is used so that you seldom need to specify font attributes yourself.
However, occasionally it becomes necessary to specify font attributes directly. One common reason is the desire to change the overall font attributes, by choosing, for example, a different font family for the main text. This alteration often can be done by simply specifying an appropriate package (see Chapter 10 for descriptions of such packages).
Another use for explicit font attributes can be to mark certain portions of the document as special — for example, to denote acronyms, example, or company names. For instance, in this book, names of packages are formatted in a sans serif font. This formatting could be achieved by surrounding the names with , but it is much better practice to define a new command (say, ) for this purpose so that additional information is included in the source document. By defining individual commands for logically different things — even those that are currently being typeset in the same way — it is easier to change the formatting later in a consistent way.
Last, but not least, in some cases you may want to override a decision taken by the document class. For example, you might want to typeset a table in a smaller size to make it fit on a page. This desire is legitimate, as document classes can format documents automatically only to a certain extent. Hand-formatting — like the insertion of page breaks— is thus often necessary to create the final version. Unfortunately, explicit formatting makes further use of the document (if changes are made) difficult and error prone. Therefore, as with all visual formatting commands, you should try to minimize the direct use of font-changing commands in a document.
9.3.1 Standard LaTeX font commands
The font used for the main text of a document is called the “main font”, “body font”, or “normal font”. It is automatically selected at the beginning of the document and in certain constructs, such as footnotes, and figures. Certain logical markup tags, such as section headings, automatically switch to a different typeface or size, depending on the document class. These changes happen behind the scenes, and the only action required of the author is to introduce the correct logical markup in the document. However, sometimes it might be desirable to manually highlight individual parts of the text, by choosing an appropriate typeface; this is done with the commands described below.
Most font-changing commands come in two forms: a command with one argument, such as , and a declarative form, such as
To change the fonts for individual words or short phrases within your document you should make use of the font commands with one argument. The declarative forms are often better in the definition of new environments or commands. For longer passages in your document, you can also use the environment form of the declaration (the declarative name without the preceding backslash), as shown in the following example. Notice that you need to be careful with spaces; e.g., if the word “typeset” is not placed immediately after the start of the environment, there would be two spaces at this point (see the end of the environment where it is done incorrectly).
9.3.2 Font commands versus declarations
We have already seen some examples of font commands that have arguments and change font attributes. These font-changing commands with arguments all start with … (except for the ommand) to emphasize that they are intended for use in normal text and to make them easily memorizable. Using such commands instead of the declarative forms has the advantage of maintaining consistency with other LaTEX constructs. They are intended for typesetting short pieces of text in a specific family, series, or shape. Table 9.2 on the facing page shows the effects of these commands.
A further advantage of these commands is that they automatically insert any necessary italic correction on either side of their argument. As a consequence, one no longer has to worry about forgetting the italic correction when changing fonts.
Only in a very few situations is this additional space wrong. For example, most typographers recommend omitting the italic correction if a small punctuation character, like a comma, directly follows the font change. As the amount of correction required is partly a matter of taste, you can define in which situations the italic correction should be suppressed. This is done by specifying the characters that should cancel a preceding italic correction in the list The default definition for this command is
9.3.3 Combining standard font commands
As already shown, the standard font-changing commands and declarations can be combined. The result is the selection of a typeface that matches the combination of all font attributes. For example:
9.3.4 Accessing all characters of a font
Sometimes it is impossible to enter a character directly from the keyboard, even though the character exists in the font. Therefore, many useful characters are accessible via command names like or , which produce “ß” and “Æ”, respectively. Some characters can also be implicitly generated from sequences of letters (this is a property of fonts) like ffi, which produces “ffi”, and —, which produces “—” in the standard TEX fonts.
In addition, the command allows you to access any character in a font by giving its number in the current encoding scheme as either a decimal, octal (preceded by ’), or hexadecimal (preceded by “) number.
9.3.5 LaTeX 2.09 font support - Compatibility for really ancient documents
More than a quarter century ago LaTEX2ε replaced LaTEX 2.09 as the standard LaTEX format. With it the two-letter font commands, such as
Old habits die hard, and even nowadays you still see them sometimes used in questions to Stack Exchange and elsewhere. For compatibility reasons the standard classes provide definitions for these commands that emulate their behavior in LaTEX 2.09 so they work if such a class is used, but modern classes not based on article or book often do not define them. So it is best to finally break with this habit and exclusively use the (now no longer) new nd companions instead.
For package developer this is a must as otherwise the package may function only in some circumstances.
9.3.6 Changing the default text fonts
To make it easier to modify the overall appearance of a document, LaTEX provides a set of built-in hooks that modify the behavior of the high-level font-changing commands discussed in the previous sections. These hooks are shown in Table 9.3, and their values can be changed by using \renewcommand. As you can see, the values for the default encoding and the default font families in Table 9.3 depend on the engine that is used.
9.3.7 relsize, scalefnt - Relative changes to the font size
Standard LaTEX offers ten predefined commands that change the overall font size (see Table 9.1 on page 666). The selected sizes depend on the document class but are otherwise absolute in value. That is, always selects the same size within a document regardless of the surrounding conditions.
However, in many situations it is desirable to change the font size relative to the current size. This can be achieved with the relsize package, originally developed by Bernie Cosell and later updated and extended for LaTEX 2ε by Donald Arseneau and Matt Swift.
The package provides the declarative command , which takes a number as its argument denoting the number of steps by which to change the size. For example, if the current size is , then would change to . If the requested number of steps is not available, then the smallest (i.e., ) or largest (i.e., ) size command is selected. This means that undoing a relative size change by negating the argument of is not guaranteed to bring you back to the original size— it is better to delimit such changes by a brace group and let LaTEX undo the modification.
The package further defines and , which are simply abbreviations for with the arguments -1 and 1, respectively. Convenient variants are and , whose argument is the text to reduce or enlarge in size. These four commands take as an optional argument the number of steps to change if something different from 1 (the default) is needed.