转载自:http://en.wikibooks.org/wiki/LaTeX/Fonts


Fonts are a complex topic. For common documents, only Font familiesEmphasizing text, and Font encoding are really needed. The other sections are more useful to macro writers or for very specific needs.

Introduction

The digital fonts have a long and intricate history. See Adobe Font Metrics for some more details.

Originally TeX was conceived to use its own font system, MetaFont, designed by D. Knuth. The default font family for TeX and friends is called Computer Modern. These high quality fonts are scalable, and have a wide range of typographical fine tuning capabilities.

Standard tex compilers will let you use other fonts. There are many different font types, such as PostScript Type1/Type3 fonts and bitmap fonts. Type1 are outline fonts (vector graphics) which are commonly used by pdftex. Bitmap fonts are raster graphics, and usually have very poor quality, which can easily be seen when zooming or printing a document. Type3 is a superset of Type1 and has more functionalities from Postscript, such as embedding raster graphics. In the TeX world, Type3 fonts are often used to embed bitmap fonts.

It should be noticed that fonts get generated the first time they are required, hence the long compilation time.

However, MetaFont is internally a quite complex font system, and the most popular font systems as of this day are Truetype font (ttf) andOpenType font (otf). With modern TeX compilers such as xetex and luatex it is possible to make use of such fonts in LaTeX documents. If you want/have to stick with the standard compilers, the aforementioned font types must first be converted and made available to LaTeX (e.g. converted to Type1 fonts). The external links section below has some useful resources.

In LaTeX, there are many ways to specify and control fonts. It is a very complex matter in typography.

Font families

There are many font families e.g. Computer Modern, Times, Arial and Courier. Those families can be grouped into three main categories: roman (rm) or serif, sans serif (sf) and monospace (tt) (see Typeface for more details). Each font family comes with the default design which falls into one of those categories; however, it is interchangeable among them. Computer Modern Roman is the default font family for LaTeX. Fonts in each family also have different properties (size, shape, weight, etc.). Families are meant to be consistent, so it is highly discouraged to change fonts individually rather than the whole family.

The three families are defined by their respective variables:

  • \rmdefault
  • \sfdefault
  • \ttdefault

The default family is contained in the \familydefault variable, and it is meant to have one of the three aforementioned variables as value. The default is defined like the following assignment:

\renewcommand*{\familydefault}{\sfdefault}

This will turn all the part of the document using the default font to the default sans serif, which is Computer Modern Sans Serif if you did not change the default font.

Changing font families usually works in two steps:

  1. First specify which family you want to change (rm, sf or tt).
  2. Second specify the new default family if it is not rm.

Mathematical fonts is a more complex matter. Fonts may come with a package that will take care of defining all three families plus the math fonts. You can do it by yourself, in which case you do not have to load any package.

Below is an example[1] that demonstrates how to change a specific family.

% Default font (\familydefault = \rmdefault = Computer Modern Roman)
Lorem ipsum dolor sit amet, consectitur adipiscing elit.

% Palatino font (ppl must be installed).
\renewcommand*\rmdefault{ppl}
Lorem ipsum dolor sit amet, consectitur adipiscing elit.

% Iwona font (iwona must be installed).
\renewcommand*\rmdefault{iwona}
Lorem ipsum dolor sit amet, consectitur adipiscing elit.

LaTeX font example.png

The three default family font variables and the \familydefault variable should not be confused with their respective switch:

  • \normalfont
  • \rmfamily
  • \sffamily
  • \ttfamily

Emphasizing Text

In order to add some emphasis to a word or a phrase, the simplest (and the most correct) way is to use the \emph{text} command.

I want to \emph{emphasize} a word.

Emph.png

Note that the \emph command is dynamic: if you emphasize a word which is already in an emphasized sentence, it will be reverted to the upright font.

\emph{In this emphasized sentence, there is an emphasized \emph{word} which looks upright.}

In this emphasized sentence, there is an emphasized wordwhich looks upright.

Font encoding

character is a sequence of bytes, and should not be confused with its representation, the glyph, which is what the reader sees. So the character 'a' has different representations following the used font, for example the upright version, the italic version, various weights and heights, and so on.

Upon compilation, tex will have to choose the right font glyph for every character. This is what is called font encoding. The default LaTeX font encoding is OT1, the encoding of the original Computer Modern TeX text fonts. It contains only 128 characters, many from ASCII, but leaving out some others and including a number that are not in ASCII. When accented characters are required, TeX creates them by combining a normal character with an accent. While the resulting output looks perfect, this approach has some caveats.

  • It stops the automatic hyphenation from working inside words containing accented characters.
  • Searches for words with accents in PDFs will fail.
  • Extracting ('e.g.' copy paste) the umlaut 'Ä' via a PDF viewer actually extracts the two characters '"A'.
  • Besides, some of Latin letters could not be created by combining a normal character with an accent, to say nothing about letters of non-Latin alphabets, such as Greek or Cyrillic.

To overcome these shortcomings, several 8-bit CM-like font sets were created. Extended Cork (EC) fonts in T1 encoding contains letters and punctuation characters for most of the European languages based on Latin script. The LH font set contains letters necessary to typeset documents in languages using Cyrillic script. Because of the large number of Cyrillic glyphs, they are arranged into four font encodings—T2A, T2B, T2C, and X2. The CB bundle contains fonts in LGR encoding for the composition of Greek text. By using these fonts you can improve/enable hyphenation in non-English documents. Another advantage of using new CM-like fonts is that they provide fonts of CM families in all weights, shapes, and optically scaled font sizes.

All this is not possible with OT1, that's why you may want to change the font encoding of your document.

Note that changing the font encoding will have some requirements over the fonts being used. The default Computer Modern font does not support T1. You will need Computer Modern Super (cm-super) or Latin Modern (lmodern), which are Computer Modern-like fonts with T1 support. If you have none of these, it is quite frequent (depends on your TeX installation) that tex chooses a Type3 font such as the Type3 EC, which is a bitmap font. Bitmap fonts look rather ugly when zoomed or printed.

The fontenc package tells LaTeX what font encoding to use. Font encoding is set with:

\usepackage['encoding']{fontenc}

where encoding is the font encoding. It is possible to load several encodings simultaneously.

There is nothing to change in your document to use CM Super fonts (assuming they are installed), they will get loaded automatically if you use T1 encoding. For lmodern, you will need to load the package after the T1 encoding has been set:

\usepackage[T1]{fontenc}
\usepackage{lmodern
}

The package ae (almost European) is obsolete. It provided some workarounds for hyphenation of words with special characters. These are not necessary any more with fonts like lmodern. Using the ae package leads to text encoding problems in PDF files generated via pdflatex(e.g. text extraction and searching), besides typographic issues.

Font styles

Each family has its own font characteristics (such as italic and bold), also known as font styles, or font properties.

Font styles are usually implemented with different font files. So it is possible to build a new font family by specifying the font styles of different font families.

Shapes

The following table lists the commands you will need to access the typical font shapes:

LaTeX commandEquivalent toOutput styleRemarks
\textnormal{...} {\normalfont...} document font family This is the default or normal font.
\emph{...} {\em ...} emphasis Typically italics. Using emph{} inside of italic text removes the italics on the emphasized text.
\textrm{...} {\rmfamily...} roman font family  
\textsf{...} {\sffamily...} sans serif font family  
\texttt{...} {\ttfamily...} teletypefont family This is a fixed-width or monospace font.
\textup{...} {\upshape ...} upright shape The same as the normal typeface.
\textit{...} {\itshape ...} italic shape  
\textsl{...} {\slshape ...} slanted shape A skewed version of the normal typeface (similar to, but slightly different from, italics).
\textsc{...} {\scshape ...} Small Capitals  
\uppercase{...}   UPPERCASE (ALL CAPS) Also \lowercase. There are some caveats, though; see here.
\textbf{...} {\bfseries...} bold  
\textmd{...} {\mdseries...} medium weight A font weight in between normal and bold.
\textlf{...} {\lfseries...} light A font weight lighter than normal. Not supported by all typefaces.

The commands in column two are not entirely equivalent to the commands in column one: They do not correct spacing after the selected font style has ended. The commands in column one are therefore in general recommended.

You may have noticed the absence of underline. This is because underlining is not recommended for typographic reasons (it weighs the text down). You should use emph instead. However underlining text provides a useful extra form of emphasis during the editing process, for example to draw attention to changes. Although underlining is available via the \underline{...} command, text underlined in this way will not break properly. This functionality has to be added with the ulem (underline emphasis) package. Stick \usepackage{ulem} in your preamble. By default, this overrides the \emph command with the underline rather than the italic style. It is unlikely that you wish this to be the desired effect, so it is better to stop ulem taking over \emph and simply call the underline command as and when it is needed.

  • To restore the usual \emph formatting, add \normalem straight after the document environment begins. Alternatively, use\usepackage[normalem]{ulem}.
  • To underline, use \uline{...}.
  • To add a wavy underline, use \uwave{...}.
  • For a strike-out (strikethrough), use \sout{...}.
  • For a slash through each individual character \xout{...}.

Some font styles are not compatible one with the other. But some extra packages will fill this hole. For bold small capitals, you might want to use:

\usepackage{bold-extra}
% ...
\textsc{ \textbf{This is bold small capitals} }

Sizing text

To apply different font sizes, simply follow the commands on this table:

Latex sizes table.png

These commands change the size within a given scope, so for instance {\Large some words} will change the size of only some words, and does not affect the font in the rest of the document. It will work for most parts of the text.

{\Large\tableofcontents}

These commands cannot be used in math mode. However, part of a formula may be set in a different size by using an \mbox command containing the size command. The new size takes effect immediately after the size command; if an entire paragraph or unit is set in a certain size, the size command should include the blank line or the \end{...} which delimits the unit.

The default for \normalsize is 10 point (option 10pt), but it may differ for some Document Styles or their options. The actual size produced by these commands also depends on the Document Style and, in some styles, more than one of these size commands may produce the same actual size.

Note that the font size definitions are set by the document class. Depending on the document style the actual font size may differ from that listed above. And not every document class has unique sizes for all 10 size commands.

Absolute Point Sizes, [10pt] being default
sizestandard classes, procAMS classes, memoirslidesbeamer
[10pt][11pt][12pt][10pt][11pt][12pt][10pt][11pt][12pt]
\tiny 6.80565 7.33325 7.33325 7.33325 7.97224 8.50012 17.27505 5.31258 6.37509 6.37509
\scriptsize 7.97224 8.50012 8.50012 7.97224 8.50012 9.24994 20.73755 7.43760 8.50012 8.50012
\footnotesize 8.50012 9.24994 10.00002 8.50012 9.24994 10.00002 20.73755 8.50012 9.24994 10.00002
\small 9.24994 10.00002 10.95003 9.24994 10.00002 10.95003 20.73755 9.24994 10.00002 10.95003
\normalsize 10.00002 10.95003 11.74988 10.00002 10.95003 11.74988 24.88382 10.00002 10.95003 11.74988
\large 11.74988 11.74988 14.09984 10.95003 11.74988 14.09984 29.86258 11.74988 11.74988 14.09984
\Large 14.09984 14.09984 15.84985 11.74988 14.09984 15.84985 35.82510 14.09984 14.09984 16.24988
\LARGE 15.84985 15.84985 19.02350 14.09984 15.84985 19.02350 43.00012 16.24988 16.24988 19.50362
\huge 19.02350 19.02350 22.82086 15.84985 19.02350 22.82086 51.60014 19.50362 19.50362 23.39682
\Huge 22.82086 22.82086 22.82086 19.02350 22.82086 22.82086 51.60014 23.39682 23.39682 23.39682

As a technical note, points in TeX follow the standard American point size in which 1 pt is approximately 0.35136 mm. The standard point size used in most modern computer programs (known as the desktop publishing point or PostScript point) has 1 pt equal to approximately 0.3527 mm while the standard European point size (known as the Didot point) had 1 pt equal to approximately 0.37597151 mm (see: point (typography)).

Local font selection

You can change font for a specific part of the text. There are four font properties you can change.

\fontencoding
The font encoding, such as OT1 (TeX default) or T1 (extended characters support, better PDF support, widely used).
\fontfamily
The font family.
\fontseries
The series: l=light, m=medium, b=bold, bx=very bold.
\fontshape
The shape: it=italic, n=normal, sl=slanted, sc=small capitals.

{
\fontfamily{anttlc}\selectfont
Some text in anttlc...
}

The \selectfont command is mandatory, otherwise the font will not be changed. It is highly recommended to enclose the command in a group to cleanly return to the previous font selection when desired.

You can use all these commands in a row:

{
\fontencoding{T1}\fontfamily{anttlc}\fontseries{m}\fontshape{n}\selectfont
Some text in anttlc...
}

The default values are stored in \encodingdefault\familydefault\seriesdefault and \shapedefault. Setting back the default font properties can be done with

\fontencoding{\encodingdefault}
\fontfamily{\familydefault}
\fontseries{\seriesdefault}
\fontshape{\shapedefault
}
\selectfont

For short, you can use the \usefont{<encoding>}{<family>}{<series>}{<shape>} command.

\usefont{T1}{cmr}{m}{n} % Computer Modern Roman (TeX default) in T1 encoding. May lead to bad text quality if you do not have cm-super installed.
\usefont{T1}{phv}{m}{sc} % phv family (sans serif) medium small capitals.
\usefont{T1}{ptm}{b}{it} % ptm family bold italic
\usefont{U}{pzd}{m}{n}   % ...

Arbitrary font size[edit]

The \tiny...\Huge commands are often enough for most contents. These are fixed sizes however. In most document processors, you can usually choose any size for any font. This is because the characters actually get magnified. If it usually looks correct for medium sizes, it will look odd at extreme sizes because of an unbalanced thickness. In TeX it is possible to change the magnification of anything, but highly discouraged for the aforementioned reason. Changing the font size is made by changing the font file. Yes, there is a file for every size: cmr10 for Computer Modern Roman 10pt, cmr12 for Computer Modern Roman 12pt, etc. This ensure the characters are correctly balanced and remain readable at all defined sizes.

You may choose a particular font size with the \fontsize{<size>}{<line space>} command. Example:

{\fontsize{5cm}{1em}\selectfont This is big!}

If you are using the default Computer Modern font with OT1 encoding, you may get the following message:

LaTeX Font Warning: Font shape `OT1/cmr/m/n' in size <142.26378> not available
(Font)              size <24.88> substituted on input line 103.

In that case you will notice that the font size cannot be changed beyond \tiny and \Huge. You must switch to a more sizable font, e.g.

\usepackage[T1]{fontenc}
\usepackage{lmodern
}

Finding fonts

You will find a huge font directory along examples and configurations at TUG Font Catalogue.

Using arbitrary system fonts

If you use the XeTeX or LuaTeX engine and the fontspec package, you'll be able to use any font installed in the system effortlessly. XeTeX also allows using OpenType technology of modern fonts like specifying alternate glyphs and optical size variants. XeTeX also usesUnicode by default, which might be helpful for font issues.

To use the fonts, simply load the fontspec package and set the font:

\documentclass{article}

\usepackage{fontspec}
\setmainfont{Arial}

\begin{document}
Lorem ipsum...
\end{document
}

Then compile the document with xelatex or lualatex. Note that you can only generate .pdf files, and that you need a sufficiently new TeX distribution (TeX Live 2009 should work for XeTeX and Tex Live 2010 for LuaTeX). Also you should not load the inputenc or fontencpackage. Instead make sure that your document is encoded as UTF-8 and load fontspec, which will take care of the font encoding. To make your document support both pdflatex and xelatex/lualatex you can use the \ifxetex\ifluatex macro from the ifxetexifluatexpackage. For example for xelatex

\documentclass{article}
\usepackage{ifxetex}

\ifxetex
  \usepackage{fontspec}
  \defaultfontfeatures{Ligatures=TeX
} % To support LaTeX quoting style
  \setromanfont{Hoefler Text}
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\fi

\begin{document}
Lorem ipsum...
\end{document
}

PDF fonts and properties

PDF documents have the capability to embed font files. It makes them portable, hence the name Portable Document Format.

Many PDF viewers have a Properties feature to list embedded fonts and document metadata.

Many Unix systems make use of the poppler tool set which features pdfinfo to list PDF metadata, and pdffonts to list embedded fonts.

Useful websites

TrueType (ttf) fonts

References

  1. Jump up found at the Google discussion group latexlovers

用户登录