Font Switching

From Wiki
Revision as of 20:14, 30 September 2013 by Huttarl (talk | contribs) (Reworded to explain how the quick font switch commands don't change the body font, just the effective font)
Jump to navigation Jump to search

< Basic Text Formatting | Fonts | Font Sizes >

In ConTeXt there are four ways to switch fonts:

Font styles and alternatives

There are three main types of font styles: serif, sans serif, and teletype. To switch between these styles, use \rm for serif, \ss for sans serif, and \tt for teletype.

Each of these styles comes in different alternatives: normal, bold, italic, slanted, bold-italic, bold-slanted, and small-capped. To switch to a different alternative, use \tf ("typeface") for normal, \bf for bold, \it for italic, \sl for slanted, \bi for bold-italic, \bs for bold-slanted, and \sc for small-capped.

You can generally combine font styles with alternatives, so if you want to switch to bold sans serif, you can use either \bf\ss or \ss\bf.

There is a font switch \em to emphasize text. This is somewhat special: it does automatic italic correction and changes the alternative depending on the current alternative. For example, if the current font alternative is normal (i.e. upright), \em switches to slanted; and if the current font alternative is slanted, \em switches to normal.

ConTeXt uses the Latin Modern fonts by default; these fonts look similar to the original Computer Modern fonts, but have a much larger character repertoire. As it happens, in the Latin Modern (and Computer Modern) fonts, the slanted font does not stand out from the upright font enough for some tastes; so, many people prefer to use the italic font for emphasis. To do that use


A font switch remains valid for the rest of the group. So, if you want to temporarily switch to a different font, use the font style command inside a group. The easiest way to start a group is to enclose the text within braces (also called curly brackets), for example

This is serif text
{\ss This is sans serif}
This is serif again
{\tt and this is typewriter}
And serif again

which gives (notice the braces in the above lines)

Font sizes

Occasionally one needs to use a font size different from the bodyfont. ConTeXt provides two series of commands for that. For a larger font size, you can use \tfa to scale the font size by a factor of 1.2, \tfb to scale by a factor of (1.2)^2 = 1.44, \tfc to scale by (1.2)^3 = 1.728 and \tfd to scale by (1.2)^4 = 2.074. The scale is relative to the current bodyfont size.

To decrease the font size, you can use \tfx to scale the font by a factor of 0.8 and \tfxx to scale by a factor of 0.6.

The mapping of particular command suffixes (a, x, etc.) and current bodyfont size to effective font size can be changed by \definebodyfontenvironment. For example, if you want \tfa to set the effective font size to 12pt when the bodyfont size is 10pt, and to set the effective font size to 14pt when the bodyfont size is 11pt, then add

\definebodyfontenvironment [10pt] [a=12pt]
\definebodyfontenvironment [11pt] [a=14pt]

\definebodyfontenvironment is described in detail in the ConTeXt manual and the font-ini.tex source file.

Font size can be combined with font styles. As a shortcut, you can use \bfa to get bold font scaled by 1.2, \bfx to get a bold font scaled by 0.8 and similar commands for other font styles.

These quick font switches are meant for changing the font style, alternative, or size of a few words: they do not change the bodyfont, so they don't affect interline spacing or math font sizes. So, if you want to change the font size of an entire paragraph, use \switchtobodyfont described below in Complete Font Change. However, it is fine to use them as style directives in setup commands, that is, using them as an option for style=... in any setup command that accepts style option.

Mnemonic font switches

While learning a document markup language like ConTeXt, it can be hard to remember all the commands. ConTeXt provides other, easy-to-remember font switches. So for bold you can use \bold, for italic you can use \italic, for slanted you can use \slanted, and so on. You can probably guess what the following do:

\normal \slanted
\boldslanted \slantedbold
\bolditalic \italicbold
\small \smallnormal
\smallbold \smallslanted
\smallboldslanted \smallslantedbold
\smallbolditalic \smallitalicbold
\sans \sansserif
\sansbold \smallcaps

In addition, the commands \smallbodyfont and \bigbodyfont can be used to change the font size.

These mnemonic font switches are pretty smart. You can either use them as font style switches inside a { group }, or as a font changing command that takes an argument. For example,

This is {\bold bold} and so is \bold{this}. But this is not.

These mnemonic font switches can also be used for all style=... options, and while using them as style options, you can just give the command name without the backslash. For example:


Complete font (bodyfont) change

If you need to change to a different font size and take care of interline spacing, you can change the bodyfont by using \switchtobodyfont. For example, to switch to 12pt you can use \switchtobodyfont[12pt].

ConTeXt provides two relative sizes, called big and small. So, to increase the bodyfont size, use \switchtobodyfont[big] (or \setbigbodyfont), and to decrease the bodyfont size, use \switchtobodyfont[small] (or \setsmallbodyfont). The exact size used for big and small can be set using \definebodyfontenvironment.

The \setupbodyfont command accepts all the same arguments as \switchtobodyfont. The difference between the two is that \setupbodyfont also changes the font for headers, footers and other page markings, while \switchtobodyfont does not. So you should use \setupbodyfont for global font definitions to apply to the whole document, and \switchtobodyfont for local font changes (i.e. changes to the running text only). The effect of \switchtobodyfont can be localized within a group as usual.

Different typefaces

So far we have discussed style and size changes within a given typeface family. If you want to use a different typeface altogether, such as Times or Palatino, the Pragma web site has recipes covering all the commonly available typefaces [1], while a separate manual describes how to write support for new typefaces [2].

The recipes as given work with the standalone ConTeXt distribution, but not with TeX Live et al. [3] explains why ConText uses separate font metrics, and gives some differences between the sets. To use the recipes with other distributions, try adding one of \usetypescript[berry][ec] or \usetypescript[adobekb][ec].


There are many other ways of choosing font styles in ConTeXt. If these basic styles do not satisfy your needs, have a look at the manuals, or ask on the ConTeXt Mailing Lists.