10

Hello there:) Could someone show me how to select fonts (serif: regular/bold/italic/bolditalic/smallcaps, sans: regular/bold/italic) for entire document using fontspec and font's filenames. And at least one decorative font for special occasions (not for entire doc) and one for equations. Should I set font families or not and what's the difference? I'm fighting with fontspec manual and I see several options and I'm totally lost. I found this:

\fontspec
[ BoldFont = texgyrepagella-bold.otf ,
ItalicFont = texgyrepagella-italic.otf ,
BoldItalicFont = texgyrepagella-bolditalic.otf ]
{texgyrepagella-regular.otf}

But how to set other fonts? Where can I obtain examples?

  • 1
    To set document-wide fonts, use \setmainfont, \setsansfont and \setmonofont. You can use the normal font name instead of a file name. For more information, see the fontspec package documentation. – ChrisS Oct 15 '13 at 2:22
16

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}

output of code

  • How to do it using exact font's filenames as I asked? I need to define path to those fonts, they are in other application's directory. For example how to set \setsansfont using font's filenames for regular/bold/italic/bolditalic/smallcaps. And what's the difference between {\cursive ...} and \cursive{...} – user38356 Oct 16 '13 at 8:57
  • To Alan Munn. I have read answer you mentioned. But still, how to set \setsansfont using font's filenames for regular/bold/italic/bolditalic/smallcaps. I don't want to use \fontspec I want \setsansfont I want to set family based on exact font's filenames . Can you provide me a working example? And why I shouldn't use \fontspec macro directly? And can I use \cursive{...} instead of {\cursive ...} without \DeclareTextFontCommand or \newcommand? And what is the difference? I prefer \cursive{...}, but maybe it is worst method? I'm new to Latex. Can you explain it? Help please. I use Luatex, is it – user38409 Oct 17 '13 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.