Typically fontspec
handles the correct assignment of the various bold, italic, etc. faces based on the main name of the font, so you shouldn't have to do this separately, nor should you need to use the actual file names. Instead you should usually use the name of the font as it would appear in e.g. a desktop application on your system.
There are three main general font selection commands that are important to know about in fontspec
:
\setmainfont[options]{<fontname>} % sets the Roman font for the document
\setsansfont[options]{<fontname>} % sets the Sans Serif font for the document
\setmonofont[options]{<fontname>} % sets the Monospaced font for the document
If you need to use a font for special purposes, generally shouldn't need to the use \fontspec
macro directly. Instead, you should use:
\newfontfamily\foo[options]{<fontname>}
Then if you need to use that font you would just use:
{\foo Some text in the special font}
This will assign the macro \foo
to the special font. This macro behaves exactly like other font family commands in that it is a switch (not a command that takes an argument) and should generally be enclosed in a group (either by braces or by creating an environment.) If you want to create an argument-taking command you can add the following:
\DeclareTextFontCommand{\textfoo}{\foo}
which will allow you to use \textfoo{<some text>}
to make <text>
in the \foo
font.
If you absolutely need to load fonts with their filenames, there are detailed instructions in the answer to the following question:
Note that all of the font loading commands described in the present answer (\setmainfont
, \setsansfont
, etc.) can be used in exactly the same way as described for the \fontspec
command in that answer.
Here's a complete example. Per your request, I've included in the example loading one of the fonts using direct filenames. In order to understand the code, please read the comments carefully. In order to load fonts by name, you need to know the exact names of each of the separate font files; these will be different for different fonts, and are usually case sensitive. In the example document, I've put the sans font TeX Gyre Adventor in a local directory called localfonts
and loaded it by name. This document can be compiled with either XeLaTeX or LuaLaTeX.
% !TEX TS-program = LuaLaTeX
\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{parskip}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{TeX Gyre Pagella}
% Now load the next font using its name and an explicit path
% The final / in the path name is required!
% In this example, I have created a directory called localfonts which
% is inside the directory containing the source document.
% So the path is relative to the source directory.
% You can also use a full path if you like.
% The TeX Gyre fonts are named "texgyreadventor-regular.otf",
% "texgyreadventor-italic.otf" etc. so this scheme is used to form the
% template for assigning the different font faces to the file name.
% The main name of the font goes in the {} argument, and the different
% faces go into the optional [] part of the command. The font only has four
% faces, so these are the only ones defined.
%
\setsansfont[Path=localfonts/,
Scale=MatchLowercase,
UprightFont = *-regular,
BoldFont = *-bold,
ItalicFont = *-italic,
BoldItalicFont = *-bolditalic
]{texgyreadventor}
\setmonofont[Scale=MatchLowercase]{Linux Biolinum O}
% Now create a new font family for the special font.
% This creates a font switch macro \cursivefont which works
% like \sffamily
\newfontfamily\cursivefont[Scale=MatchLowercase]{TeX Gyre Chorus}
% Now create an equivalent command that takes an argument
% This creates a \cursive{} macro.
\DeclareTextFontCommand{\cursive}{\cursivefont}
\begin{document}
The main font will be Pagella. The \textbf{bold} and \emph{italics} work as
you would expect.
{\sffamily You can use \verb|\sffamily| as normal to \emph{switch} to the
\textbf{sans font}. Of course verbatim text will show up in the mono font as the
verbatim section of this sentence did.}
{\cursivefont This text is in the cursive font.}
\cursive{This is also in cursive}
\end{document}
\setmainfont
,\setsansfont
and\setmonofont
. You can use the normal font name instead of a file name. For more information, see the fontspec package documentation.