Debian Codegraph @

texmf-dist/source/latex/fontspec/fontspec-doc-fontsel.tex

raw
1 %%^^A%% fontspec-doc-fontsel.tex -- part of FONTSPEC <latex3.github.io/fontspec>
2
3 \documentclass[a4paper]{l3doc}
4 \usepackage{fontspec-doc-style}
5 \showexamplesfalse
6 \begin{document}
7
8 \part{General font selection}
9
10 \section{Main commands}
11 \label{sec:main-cmd}
12
13 This section concerns the variety of commands that can be used to select
14 fonts.
15
16 \bigskip
17 \cmdbox{%
18 \CMD{\string\setmainfont}\marg{font}\oarg{font features}\\
19 \CMD{\string\setsansfont}{\color[gray]{0.5}\marg{font}\oarg{font features}}\\
20 \CMD{\string\setmonofont}{\color[gray]{0.5}\marg{font}\oarg{font features}}
21 }
22
23 These are the main font-selecting commands of this package which select the standard fonts used in a document, as shown in \exref{fontload}.
24 Here, the scales of the fonts have been chosen to equalise their
25 lowercase letter heights. The \feat{Scale} font feature will be discussed
26 further in \vref{sec:font-ind-features}, including methods for automatic
27 scaling.
28 Note that further options may need to be added to select appropriate bold/italic fonts,
29 but this shows the main idea.
30
31 Note that while these commands all look and behave largely identically, the default setup for font loading automatically adds the |Ligatures=TeX| feature for the \cs{setmainfont} and \cs{setsansfont} commands.
32 These defaults (and further customisations possible) are discussed in \vref{sec:defaults}.
33
34 \bigskip
35 \cmdbox{%
36 \CMD{\string\newfontfamily}\meta{cmd}\marg{font}\oarg{font features}\\
37 \CMD{\string\setfontfamily}{\color[gray]{0.5}\meta{cmd}\marg{font}\oarg{font features}}\\
38 \CMD{\string\renewfontfamily}{\color[gray]{0.5}\meta{cmd}\marg{font}\oarg{font features}}\\
39 \CMD{\string\providefontfamily}{\color[gray]{0.5}\meta{cmd}\marg{font}\oarg{font features}}
40 }
41
42 These commands define new font family commands (like \cs{rmfamily}).
43 The |new| command checks if \meta{cmd} has been defined, and issues an error if so.
44 The |renew| command checks if \meta{cmd} has been defined, and issues an error if not.
45 The |provide| command checks if \meta{cmd} has been defined, and silently aborts if so.
46 The |set| command never checks; use at your own risk.
47
48
49
50 \bigskip
51 \cmdbox{%
52 \CMD{\string\fontspec}\marg{font}\oarg{font features}
53 }
54
55 The plain \cs{fontspec} command is not generally recommended for document use. It is an
56 ad hoc command best suited for testing and loading fonts on a one-off basis.
57
58 All of the commands listed above accept comma-separated
59 \meta{font feature}=\meta{option} lists; these are described later:
60 \begin{itemize}
61 \item For general font features, see \vref{sec:font-ind-features}
62 \item For OpenType fonts, see Part~\vref{sec:opentype-features}
63 \item For \XeTeX-only general font features, see Part~\vref{sec:xetex-features}
64 \item For \LuaTeX-only general font features, see Part~\vref{sec:luatex-features}
65 \item For features for \AAT\ fonts in \XeTeX, see \vref{sec:aat-features}
66 \end{itemize}
67
68 \begin{Lexample}{fontload}{Loading the default, sans serif, and monospaced fonts.}
69 \setmainfont{texgyrebonum-regular.otf}
70 \setsansfont{lmsans10-regular.otf}[Scale=MatchLowercase]
71 \setmonofont{Inconsolatazi4-Regular.otf}[Scale=MatchLowercase]
72
73 \rmfamily Pack my box with five dozen liquor jugs\par
74 \sffamily Pack my box with five dozen liquor jugs\par
75 \ttfamily Pack my box with five dozen liquor jugs
76 \end{Lexample}
77
78 \section{Font selection}
79
80 In both \LuaTeX\ and \XeTeX, fonts can be selected (using the \meta{font} argument in \ref{sec:main-cmd}) either by `font name' or by `file name', but there are some differences in how each engine finds and selects fonts --- don't be too surprised if a font invocation in one engine needs correction to work in the other.
81
82
83 \subsection{By font name}
84
85 Fonts known to \LuaTeX\ or \XeTeX\ may be loaded by their standard names as
86 you'd speak them out loud, such as \emph{Times New Roman} or
87 \emph{Adobe Garamond}.
88 `Known to' in this case generally means `exists in a standard fonts location'
89 such as |~/Library/Fonts| on \MacOSX, or |C:\Windows\Fonts| on Windows.
90 In \LuaTeX, fonts found in the \textsc{texmf} tree can also be loaded by name.
91 In \XeTeX, fonts found in the \textsc{texmf} tree can be loaded in Windows and Linux,
92 but not on \MacOSX.
93
94 The simplest example might be something like
95 \begin{Verbatim}
96 \setmainfont{Cambria}[ ... ]
97 \end{Verbatim}
98 in which the bold and italic fonts will be found automatically
99 (if they exist) and are immediately accessible with the usual
100 \cs{textit} and \cs{textbf} commands.
101
102 The `font name' can be found in various ways, such as by looking in the name listed in a application like \emph{Font Book} on Mac~OS~X.
103 Alternatively, \TeX{}Live contains the \texttt{otfinfo} command line program, which can query this information; for example:
104 \begin{Verbatim}
105 otfinfo -i `kpsewhich lmroman10-regular.otf`
106 \end{Verbatim}
107 results in a line that reads:
108 \begin{Verbatim}
109 Preferred family: Latin Modern Roman
110 \end{Verbatim}
111 (The `preferred family' name is usually better than the `family' name.)
112
113 \paragraph{\LuaTeX\ users only}
114 In order to load fonts by their name rather than by their filename (\eg,
115 `Latin Modern Roman' instead of `ec-lmr10'), you may need to run the script
116 \texttt{luaotfload-tool}, which is distributed with the \pkg{luaotfload}
117 package. Note that if you do not execute this script beforehand, the first
118 time you attempt to typeset the process will pause for (up to) several
119 minutes. (But only the first time.)
120 Please see the \pkg{luaotfload} documentation for more information.
121
122
123 \subsection{By file name}
124 \label{sec:by-file-name}
125
126 \XeTeX\ and \LuaTeX\ also allow fonts to be loaded by file name instead of font name.
127 When you have a very large collection of fonts, you will sometimes not
128 wish to have them all installed in your system's font directories.
129 In this case, it is more convenient to load them from a different location on your disk.
130 This technique is also necessary in \XeTeX\ when loading OpenType fonts that are present within your \TeX\ distribution, such as \path{/usr/local/texlive/2013/texmf-dist/fonts/opentype/public}.
131 Fonts in such locations are visible to \XeTeX\ but cannot be loaded by font name, only file name; \LuaTeX\ does not have this restriction.
132 (If you for some reason want to restrict the fonts to the ones provided by your \TeX\ distribution even though you are using \LuaTeX\ you can use the \DescribeOption{KpseOnly}\texttt{KpseOnly} option)
133
134 When selecting fonts by file name, any font that can be found in the default
135 search paths may be used directly (including in the current directory)
136 without having to explicitly define the location of the font file on disk.
137
138 Fonts selected by filename must include bold and italic variants explicitly,
139 unless a \texttt{.fontspec} file is supplied for the font family (see \ref{sec:fontspecfile}).
140 We'll give some first examples specifying everything explicitly:
141 \begin{Verbatim}
142 \setmainfont{texgyrepagella-regular.otf}[
143 BoldFont = texgyrepagella-bold.otf ,
144 ItalicFont = texgyrepagella-italic.otf ,
145 BoldItalicFont = texgyrepagella-bolditalic.otf ]
146 \end{Verbatim}
147 \pkg{fontspec} knows that the font is to be selected by file name by the
148 presence of the `|.otf|' extension.
149 An alternative is to specify the extension separately, as shown following:
150 \begin{Verbatim}
151 \setmainfont{texgyrepagella-regular}[
152 Extension = .otf ,
153 BoldFont = texgyrepagella-bold ,
154 ... ]
155 \end{Verbatim}
156 If desired, an abbreviation can be applied to the font names based on the
157 mandatory `font name' argument:
158 \begin{Verbatim}
159 \setmainfont{texgyrepagella}[
160 Extension = .otf ,
161 UprightFont = *-regular ,
162 BoldFont = *-bold ,
163 ... ]
164 \end{Verbatim}
165 In this case `texgyrepagella' is no longer the name of an actual font,
166 but is used to construct the font names for each shape;
167 the |*| is replaced by `texgyrepagella'.
168 Note in this case that |UprightFont| is required for constructing the font
169 name of the normal font to use.
170
171 To load a font that is not in one of the default search paths, its location
172 in the filesystem must be specified with the |Path| feature:
173 \begin{Verbatim}
174 \setmainfont{texgyrepagella}[
175 Path = /Users/will/Fonts/ ,
176 UprightFont = *-regular ,
177 BoldFont = *-bold ,
178 ... ]
179 \end{Verbatim}
180 Note that \XeTeX\ and \LuaTeX\ are able to load the font without giving an
181 extension, but \pkg{fontspec} must know to search for the file; this can be
182 indicated by using the |Path| feature without an argument:
183 \begin{Verbatim}
184 \setmainfont{texgyrepagella-regular}[
185 Path, BoldFont = texgyrepagella-bold,
186 ... ]
187 \end{Verbatim}
188 My preference is to always be explicit and include the extension; this also allows \pkg{fontspec} to automatically identify that the font should be loaded by filename.
189
190 In previous versions of the package, the |Path| feature was also provided under the alias |ExternalLocation|, but this latter name is now deprecated and should not be used for new documents.
191
192
193 \subsection{By custom file name using a \texttt{.fontspec} file}
194 \label{sec:fontspecfile}
195
196 When \pkg{fontspec} is first asked to load a font, a font settings file is searched
197 for with the name `\meta{fontname}\texttt{.fontspec}'.\footnote{Located in the current folder or within a standard \texttt{texmf} location.}
198 If you want to \emph{disable} this feature on a per-font basis, use the \feat{IgnoreFontspecFile} font option.
199
200 The contents of this file can be used to specify font shapes and font features without having to have this information present within each document.
201 Therefore, it can be more flexible than the alternatives listed above.
202
203 When searching for this \texttt{.fontspec} file, \meta{fontname} is stripped of spaces and file extensions are omitted.
204 For example, given |\setmainfont{TeX Gyre Adventor}|, the \texttt{.fontspec} file would be
205 called \texttt{TeXGyreAdventor.fontspec}.
206 If you wanted to transparently load options for |\setmainfont{texgyreadventor-regular.otf}|, the configuration
207 file would be \texttt{texgyreadventor-regular.fontspec}.
208
209 N.B. that while spaces are stripped, the lettercase of the names should match.
210
211 This mechanism can be used to define custom names or aliases for your font collections.
212 The syntax within this file follows from the \cs{defaultfontfeatures}, defined in more detail later but mirroring the standard \pkg{fontspec} font loading syntax.
213 As an example, suppose we're defining a font family to be loaded with \verb|\setmainfont{My Charis}|.
214 The corresponding \texttt{MyCharis.fontspec} file would contain, say,
215 \begin{Verbatim}
216 \defaultfontfeatures[My Charis]
217 {
218 Extension = .ttf ,
219 UprightFont = CharisSILR,
220 BoldFont = CharisSILB,
221 ItalicFont = CharisSILI,
222 BoldItalicFont = CharisSILBI,
223 % <any other desired options>
224 }
225 \end{Verbatim}
226 The optional argument to \cs{defaultfontfeatures} must exactly match that requested by the font loading command (\verb|\setmainfont|, etc.) --- in particular note that spaces are significant here, so |\setmainfont{MyCharis}| will not `see' the default font feature setting within the \texttt{.fontspec} file.
227
228 Finally, note that options for individual font faces can also be defined in this way.
229 To continue the example above, here we colour the different faces:
230 \begin{Verbatim}
231 \defaultfontfeatures[CharisSILR]{Color=blue}
232 \defaultfontfeatures[CharisSILB]{Color=red}
233 \end{Verbatim}
234 Such configuration lines could be stored either inline inside \texttt{MyCharis.fontspec}
235 or within their own \texttt{.fontspec} files; in this way, \pkg{fontspec} is designed to
236 handle `nested' configuration options.
237
238 Where \cs{defaultfontfeatures} is being used to specify font faces by a custom name,
239 the \feat{Font} feature is used to set the filename of the font face. For example:
240 \begin{Verbatim}
241 \defaultfontfeatures[charis]
242 {
243 UprightFont = charis-regular,
244 % <other desired options for all font faces in the family>
245 }
246
247 \defaultfontfeatures[charis-regular]
248 {
249 Font = CharisSILR
250 % <other desired options just for the `upright' font>
251 }
252 \end{Verbatim}
253 The \pkg{fontspec} interface here is designed to be flexible to accommodate a variety
254 of use cases; there is more than one way to achieve the same outcome when font faces are
255 collected together into a larger font family.
256
257
258 \subsection{Querying whether a font `exists'}
259
260 \cmdbox{
261 \CMD{\string\IfFontExistsTF}\marg{font name}\marg{true branch}\marg{false branch}
262 }
263
264 The conditional \cs{IfFontExistsTF} is provided to test whether the \meta{font name} exists or is loadable.
265 If it is, the \meta{true branch} code is executed; otherwise, the \meta{false branch} code is.
266
267 This command can be slow since the engine may resort to scanning the filesystem for a missing font.
268 Nonetheless, it has been a popular request for users who wish to define `fallback fonts' for their documents for greater portability.
269
270 In this command, the syntax for the \meta{font name} is a restricted/simplified version of the font loading syntax used for \cs{fontspec} and so on.
271 Fonts to be loaded by filename are detected by the presence of an appropriate extension (|.otf|, etc.), and paths should be included inline.
272 E.g.:
273 \begin{Verbatim}
274 \IfFontExistsTF{cmr10}{T}{F}
275 \IfFontExistsTF{Times New Roman}{T}{F}
276 \IfFontExistsTF{texgyrepagella-regular.otf}{T}{F}
277 \IfFontExistsTF{/Users/will/Library/Fonts/CODE2000.TTF}{T}{F}
278 \end{Verbatim}
279
280 The \cs{IfFontExistsTF} command is a synonym for the programming interface function \cs{fontspec_font_if_exist:nTF} (\vref{sec:api}).
281
282
283
284 \section{Commands to select font families}
285
286 For cases when a specific font with a specific
287 feature set is going to be re-used many times in a document, it is inefficient
288 to keep calling \cs{fontspec} for every use. While the \cs{fontspec} command does not define
289 a new font instance after the first call, the feature options must still be
290 parsed and processed.
291
292 For this reason, new commands can be created for loading a particular font
293 family with the \cmd\newfontfamily\ command and variants,
294 outlined in \vref{sec:main-cmd} and demonstrated in \exref{nff}.
295 This macro should be used to create commands that would be used in
296 the same way as \cmd\rmfamily, for example.
297 If you would like to create a command that only changes the font
298 inside its argument (i.e., the same behaviour as \cs{emph}) define it using regular \LaTeX\
299 commands:
300 \begin{Verbatim}
301 \newcommand\textnote[1]{{\notefont #1}}
302 \textnote{This is a note.}
303 \end{Verbatim}
304 Note that the double braces are intentional; the inner pair is used to
305 delimit the scope of the font change.
306
307 \begin{Lexample}{nff}{Defining new font families.}
308 \newfontfamily\notefont{Kurier}
309 \notefont This is a \emph{note}.
310 \end{Lexample}
311
312 \emph{Comment for advanced users:}
313 The commands defined by \cs{newfontfamily} (and \cs{newfontface}; see next section) include
314 their encoding information, so even if the document is set to use a
315 legacy \TeX\ encoding, such commands will still work correctly. For example,
316 \begin{Verbatim}
317 \documentclass{article}
318 \usepackage{fontspec}
319 \newfontfamily\unicodefont{Lucida Grande}
320 \usepackage{mathpazo}
321 \usepackage[T1]{fontenc}
322 \begin{document}
323 A legacy \TeX\ font. {\unicodefont A unicode font.}
324 \end{document}
325 \end{Verbatim}
326
327
328 \section{Commands to select single font faces}
329
330 \cmdbox{%
331 \CMD{\string\newfontface}\meta{cmd}\marg{font}\oarg{font features}\\
332 \CMD{\string\setfontface}{\color[gray]{0.5}\meta{cmd}\marg{font}\oarg{font features}}\\
333 \CMD{\string\renewfontface}{\color[gray]{0.5}\meta{cmd}\marg{font}\oarg{font features}}\\
334 \CMD{\string\providefontface}{\color[gray]{0.5}\meta{cmd}\marg{font}\oarg{font features}}
335 }
336
337 Sometimes only a specific font face is desired, without accompanying italic or bold variants
338 being automatically selected.
339 This is common when selecting a font for a very particular context within the document.
340 For instance, say that a particular swash font is desired that isn't part of the document
341 font setup. \cmd\newfontface\ could be used for this purpose, shown in \exref{nfface}.
342
343 \begin{Xexample}{nfface}{Defining a single font face.}
344 \newfontface\qedfont{EBGaramond-Regular.otf}[Style=Swash]
345 \qedfont QED
346 % \emph, \textbf, etc., all don't work
347 \end{Xexample}
348
349
350 \subsection{More control over font shape selection}
351 \label{sec:bfitfonts}
352
353 \cmdbox{
354 \feat{BoldFont} = \meta{font name} \\
355 \feat{ItalicFont} = \meta{font name} \\
356 \feat{~BoldItalicFont} = \meta{font name} \\
357 \feat{SlantedFont} = \meta{font name} \\
358 \feat{BoldSlantedFont} = \meta{font name} \\
359 \feat{SwashFont} = \meta{font name} \\
360 \feat{BoldSwashFont} = \meta{font name} \\
361 \feat{SmallCapsFont} = \meta{font name} \\
362 \feat{UprightFont} = \meta{font name}
363 }
364
365 The automatic bold, italic, and bold italic font selections will not be
366 adequate for the needs of every font: while some fonts mayn't even
367 have bold or italic shapes, in which case a skilled (or lucky)
368 designer may be able to choose well-matching accompanying shapes from
369 a different font altogether, others can have a range of bold and
370 italic fonts to choose among. The \feat{BoldFont} and
371 \feat{ItalicFont} features are provided for these situations. If only
372 one of these is used, the bold italic font is requested as the
373 default from the \emph{new} font. See \exref{bff}.
374
375 \begin{Lexample}{bff}{Explicit selection of the bold font.}
376 \setmainfont{Ysabeau-Hairline.otf}%
377 [BoldFont={Ysabeau-Thin.otf}]
378 Hairline \\
379 {\itshape Hairline Italic} \\
380 {\bfseries Thin } \\
381 {\bfseries\itshape Thin Italic} \\
382 \end{Lexample}
383
384 If a bold italic shape is not defined, or you want to specify
385 \emph{both} custom bold and italic shapes, the \feat{BoldItalicFont}
386 feature is provided.
387
388
389
390 \subsubsection{Small caps shapes}
391
392 For modern OpenType fonts, small caps glyphs are included within a fontface and \pkg{fontspec} will automatically detect them
393 for use with the \cs{textsc} and \cs{scshape} commands.
394 Pre-OpenType, it was common for font families to be distributed with small caps glyphs in separate fonts, due to the limitations on the number of glyphs allowed in the PostScript Type~1 format.
395 Such fonts may be used by declaring the \feat{SmallCapsFont} for each font of the family you are specifying:
396 \begin{Verbatim}
397 \setmainfont{ <upright> }[
398 UprightFeatures = { SmallCapsFont={ <sc> } } ,
399 BoldFeatures = { SmallCapsFont={ <bf sc> } } ,
400 ItalicFeatures = { SmallCapsFont={ <it sc> } } ,
401 BoldItalicFeatures = { SmallCapsFont={ <bf it sc> } } ,
402 ]
403 Roman 123 \\ \textsc{Small caps 456}
404 \end{Verbatim}
405 For most modern fonts that have small caps as a font feature, this level of
406 control isn't generally necessary.
407
408 All of the bold, italic, and small caps fonts can be loaded with different
409 font features from the main font. See \ref{sec:bfit-feat} for details.
410 When an OpenType font is selected for |SmallCapsFont|, the small caps
411 font feature is \emph{not} automatically enabled. In this case, users
412 should write instead, if necessary,
413 \begin{Verbatim}
414 \setmainfont{...}[
415 SmallCapsFont={...},
416 SmallCapsFeatures={Letters=SmallCaps},
417 ]
418 \end{Verbatim}
419
420
421 \subsubsection{Slanted font shapes}
422
423 When a font family has both slanted \emph{and} italic shapes, these may be specified separately using the analogous features \feat{SlantedFont} and \feat{BoldSlantedFont}.
424 Without these, however, the \LaTeX\ font switches for slanted (\cs{textsl}, \cs{slshape}) will default to the italic shape.
425
426
427 \subsubsection{Swash font shapes}
428
429 Swash font shapes in a family is supported by \LaTeX's commands \cs{textsw} and \cs{swshape}. These commands assume that swash shapes are in a sense `parallel' to italic shapes --- for instance, writing both \cs{swshape} and \cs{itshape} would not result in an italic swash shape (you would get whichever was declared last).
430 The \pkg{fontspec} package adopts this approach, while recognising that OpenType fonts in theory could have any crazy combination of shapes such as `italic swash small caps'.
431 Attempting to support arbitrarily complex situations makes setup (and the code) more difficult with let's say infrequent benefit --- \pkg{fontspec}'s alternate feature selection mechanisms (such as |\addfontfeature{Style=Swash}|) can be used in such situations.
432
433 Therefore, setup is quite simple:
434 \begin{Verbatim}
435 \setmainfont{...}[
436 SwashFont = {...} ,
437 BoldSwashFont = {...} ,
438 ]
439 \end{Verbatim}
440 No assumptions are made about the |+swsh| OpenType feature availability, and if desired the `Swash' feature needs to be explicitly requested as in:
441 \begin{Verbatim}
442 \setmainfont{...}[
443 SwashFont = {...} ,
444 SwashFeatures = {Style=Swash} ,
445 ...
446 ]
447 \end{Verbatim}
448 This may become more automatic in the future.
449
450
451
452
453 \subsection{Specifically choosing the \NFSS\ family}
454
455 In \LaTeX's \NFSS, font families are defined with names such as `\texttt{ppl}' (Palatino), `\texttt{lmr}' (Latin Modern Roman), and so on, which are selected with the \cs{fontfamily} command:
456 \begin{Verbatim}
457 \fontfamily{ppl}\selectfont
458 \end{Verbatim}
459 In \pkg{fontspec}, the family names are auto-generated based on the fontname of the font; for example, writing |\fontspec{Times New Roman}| for the first time would generate an internal font family name of `\texttt{TimesNewRoman(1)}'.
460 Please note that you should not rely on the name that is generated.
461
462 In certain cases it is desirable to be able to choose this internal font family name so it can be re-used elsewhere for interacting with other packages that use the \LaTeX's font selection interface; an example might be
463 \begin{Verbatim}
464 \usepackage{fancyvrb}
465 \fvset{fontfamily=myverbatimfont}
466 \end{Verbatim}
467 To select a font for use in this way in \pkg{fontspec} use the \feat{NFSSFamily} feature:\footnote{Thanks to Luca Fascione for the example and motivation for finally implementing this feature.}
468 \begin{Verbatim}
469 \newfontfamily\verbatimfont{Inconsolata}[NFSSFamily=myverbatimfont]
470 \end{Verbatim}
471 It is then possible to write commands such as:
472 \begin{Verbatim}
473 \fontfamily{myverbatimfont}\selectfont
474 \end{Verbatim}
475 which is essentially the same as writing |\verbatimfont|, or to go back to the original example:
476 \begin{Verbatim}
477 \fvset{fontfamily=myverbatimfont}
478 \end{Verbatim}
479 Only use this feature when necessary; the in-built font switching commands that \pkg{fontspec} generates (such as |\verbatimfont| in the example above) are recommended in all other cases.
480
481 If you don't wish to explicitly set the \NFSS\ family but you would like to know what it is, an alternative mechanism for package writers is introduced as part of the \pkg{fontspec} programming interface; see the function \cs{fontspec_set_family:Nnn} for details (\vref{sec:api}).
482
483 \subsection{Choosing additional \NFSS\ font faces}
484
485 \LaTeX's font selection scheme (\NFSS) is more flexible than the \pkg{fontspec} interface discussed up until this point.
486 It assigns to each font face a \emph{family} (discussed above), a \emph{series} such as bold or light or condensed, and a \emph{shape} such as italic or slanted or small caps.
487 The \pkg{fontspec} features such as \feat{BoldFont} and so on all assign faces for the default series and shapes of the \NFSS, but it's not uncommon to have font families that have multiple weights and shapes and so on.
488
489 If you set up a regular font family with the `standard four' (upright, bold, italic, and bold italic) shapes and then want to use, say, a light font for a certain document element, many users will be perfectly happy to use \cs{newfontface}\cs{\meta{switch}} and use the resulting font \cs{\meta{switch}}.
490 In other cases, however, it is more convenient or even necessary to load additional fonts using additional \NFSS\ specifiers.
491
492 \cmdbox{
493 \texttt{FontFace = }\marg{series}\marg{shape}
494 \texttt{\char`\{} \texttt{Font = }\meta{font name} \texttt, \meta{features} \texttt{\char`\}} \\
495 \texttt{FontFace = }\marg{series}\marg{shape}\marg{font name}
496 }
497
498 The font thus specified will inherit the font features of the main font, with optional additional \meta{features} as requested.
499 (Note that the optional \marg{features} argument is still surrounded with curly braces.)
500 Multiple \feat{FontFace} commands may be used in a single declaration to specify multiple fonts.
501 As an example:
502 \begin{Verbatim}
503 \setmainfont{font1.otf}[
504 FontFace = {c}{\shapedefault}{ font2.otf } ,
505 FontFace = {c}{m}{ Font = font3.otf , Color = red }
506 ]
507 \end{Verbatim}
508 Writing |\fontseries{c}\selectfont| will result in |font2| being selected, which then followed by |\fontshape{m}\selectfont| will result in |font3| being selected (in red).
509 A font face that is defined in terms of a different series but an upright shape (|\shapedefault|, as shown above) will attempt to find a matching small caps feature and define that face as well.
510 Conversely, a font face defined in terms of a non-standard font shape will not.
511
512 There are some standards for choosing shape and series codes; the \LaTeXe\ font selection guide\footnote{\texttt{texdoc fntguide}} has a comprehensive listing.
513
514 The \feat{FontFace} command also interacts properly with the \feat{SizeFeatures} command as follows:
515 \begin{Verbatim}
516 FontFace = {c}{n}{
517 Font = lmsans10-oblique.otf ,
518 SizeFeatures = {
519 { Size = -10 , Font = lmsans8-oblique.otf } ,
520 { Size = 10-15 } ,
521 { Size = 15- , Font = lmsans17-oblique.otf } ,
522 },
523 },
524 \end{Verbatim}
525 Note that if the first \feat{Font} feature is omitted then each size needs its own inner \feat{Font} declaration.
526
527 \subsubsection{An example for \cs{strong}}
528 \label{sec:strong-example}
529
530 If you wanted to set up a font family to allow nesting of the \cs{strong}
531 to easily access increasing font weights, you might use a declaration along the following lines:
532 \begin{Verbatim}
533 \setmonofont{SourceCodePro}[
534 Extension = .otf ,
535 UprightFont = *-Light ,
536 BoldFont = *-Regular ,
537 FontFace = {k}{n}{*-Black} ,
538 ]
539 \strongfontdeclare{\bfseries,\fontseries{k}\selectfont}
540 \end{Verbatim}
541
542 Further `syntactic sugar' is planned to make this process somewhat easier.
543
544
545 \subsection{Math(s) fonts}
546
547 When \cmd\setmainfont, \cmd\setsansfont\ and \cmd\setmonofont\ are used in the
548 preamble, they also define the fonts to be used in maths mode inside the
549 \cmd\mathrm-type commands. This only occurs in the preamble because \LaTeX\
550 freezes the maths fonts after this stage of the processing. The \pkg{fontspec}
551 package must also be loaded after any maths font packages (\eg, \pkg{euler})
552 to be successful. (Actually, it is \emph{only} \pkg{euler} that is the
553 problem.\note{Speaking of \pkg{euler}, if you want to use its
554 \texttt{[mathbf]} option, it won't work, and you'll need to put this after
555 \pkg{fontspec} is loaded instead:
556 \ttfamily\cmd\AtBeginDocument\char`\{\cmd\DeclareMathAlphabet\cmd\mathbf\char`\{U\char`\}\char`\{eur\char`\}\char`\{b\char`\}\char`\{n\char`\}\char`\}})
557
558 Note that \pkg{fontspec} will not change the font for general mathematics;
559 only the upright and bold shapes will be affected.
560 To change the font used for the mathematical symbols, see either the
561 \pkg{mathspec} package or the \pkg{unicode-math} package.
562
563 Note that you may find that loading some maths packages won't be as smooth as
564 you expect since \pkg{fontspec} (and \XeTeX\ in general) breaks many of the
565 assumptions of \TeX\ as to where maths characters and accents can be found.
566 Contact me if you have troubles, but I can't guarantee to be able to fix any
567 incompatibilities. The Lucida and Euler maths fonts should be fine; for all
568 others keep an eye out for problems.
569
570 \cmdbox{
571 \cmd{\setmathrm}\marg{font name}\oarg{font features} \\
572 \cmd{\setmathsf}\marg{font name}\oarg{font features} \\
573 \cmd{\setmathtt}\marg{font name}\oarg{font features} \\
574 \cmd{\setboldmathrm}\marg{font name}\oarg{font features}
575 }
576
577 However, the default text fonts may not necessarily be the ones you
578 wish to use when typesetting maths (especially with the use of fancy
579 ligatures and so on). For this reason, you may optionally use the
580 commands above (in the same way as our other
581 \cmd\fontspec-like commands) to explicitly state which fonts to use
582 inside such commands as \cmd\mathrm. Additionally, the
583 \cmd\setboldmathrm\ command allows you define the font used for
584 \cmd\mathrm\ when in bold maths mode (which is activated with, among
585 others, \cmd\boldmath).
586
587 For example, if you were using Optima with the Euler maths font, you
588 might have this in your preamble:
589 \begin{Verbatim}
590 \usepackage{mathpazo}
591 \usepackage{fontspec}
592 \setmainfont{Optima}
593 \setmathrm{Optima}
594 \setboldmathrm[BoldFont={Optima ExtraBlack}]{Optima Bold}
595 \end{Verbatim}
596 These commands are compatible with the \pkg{unicode-math} package.
597 Having said that, \pkg{unicode-math} also defines a more general way of defining fonts to use in maths mode, so you can ignore this subsection if you're already using that package.
598
599
600 \section{Miscellaneous font selecting details}
601
602 \paragraph{The optional argument --- from v2.4}
603 For the first decade of \pkg{fontspec}'s life, optional font features were selected with a bracketed argument before the font name, as in:
604 \begin{Verbatim}
605 \setmainfont[
606 lots and lots ,
607 and more and more ,
608 an excessive number really ,
609 of font features could go here
610 ]{myfont.otf}
611 \end{Verbatim}
612 This always looked like ugly syntax to me, because the most important detail --- the name of the font --- was tucked away at the end.
613 The order of these arguments has now been reversed:
614 \begin{Verbatim}
615 \setmainfont{myfont.otf}[
616 lots and lots ,
617 and more and more ,
618 an excessive number really ,
619 of font features could go here
620 ]
621 \end{Verbatim}
622 I hope this doesn't cause any problems.
623 \begin{enumerate}
624 \item Backwards compatibility has been preserved, so either input method works.
625 \item In fact, you can write
626 \begin{Verbatim}
627 \fontspec[Ligatures=Rare]{myfont.otf}[Color=red]
628 \end{Verbatim}
629 if you really felt like it and both sets of features would be applied.
630
631 \end{enumerate}
632
633 \paragraph{Spaces}
634 \cmd\fontspec\ and \cmd\addfontfeatures\ ignore trailing spaces as
635 if it were a `naked' control sequence; \eg, `|M. \fontspec{...} N|' and
636 `|M. \fontspec{...}N|' are the same.
637
638 \end{document}
639
640 % /©
641 % ------------------------------------------------
642 % The FONTSPEC package <latex3.github.io/fontspec>
643 % ------------------------------------------------
644 % Copyright 2022-2025 The LaTeX project, LPPL "maintainer"
645 % Copyright 2004-2022 Will Robertson
646 % Copyright 2009-2015 Khaled Hosny
647 % Copyright 2013 Philipp Gesang
648 % Copyright 2013-2016 Joseph Wright
649 % ------------------------------------------------
650 % This package is free software and may be redistributed and/or modified under
651 % the conditions of the LaTeX Project Public License, version 1.3c or higher
652 % (your choice): <http://www.latex-project.org/lppl/>.
653 % ------------------------------------------------
654 % ©/