Load fonts that are in a ./fonts directory

The following solution works for both XeLaTeX and LuaLaTeX.

P. 7 of the fontspec manual says:

To load a font that is not in one of the default search paths, its location in the filesystem must be specified with the Path feature:

\fontspec [ Path = /Users/will/Fonts/, 
UprightFont = *-regular,
 BoldFont = *-bold, ...]
{texgyrepagella}

The path doesn't have to be specified absolutely, so if you have your font(s) in e.g. texsource/fonts and your source is in texsource, you can use:

\fontspec [ Path = fonts/, ... ]  { } % note that the trailing '/' is required

Since it's usually inadvisable to use the \fontspec command directly, a more usual use would be to use one of the main font selecting commands, or \newfontfamily.

\setmainfont [ Path = fonts/, ... ] { }
\setsansfont [ Path = fonts/, ... ] { }
\setmonofont [ Path = fonts/, ... ] { }
\newfontfamily [ Path = fonts/, ... ] { }

Note that the part that follows the specification for the UprightFont, etc. must match the names of the actual font you are using. So for example, if your font files are named

MyFont-Roman.ttf and MyFont-Italic.ttf

the command to load them would use:

\fontspec [Path = fonts/,
    UprightFont = *-Roman,
    ItalicFont = *-Italic
 ]
{MyFont}

etc.

Remember you don't need to use the \fontspec command specifically. Any other font selection command from fontspec such as \setmainfont or \newfontfamily can also be used with a Path specification as described earlier.

Unless you have a specific need for the fonts to be local, it's easiest just to install them into your system (on a Mac, /Library/Fonts) and they will be found automatically.


I'll add as a bit of an aside, if you've read this and are still having problems.. the ttf that the code looks for is the same as the name of the font specified in the tex doc. So if the font name has spaces the ttf filename has to have spaces to! e.g. I had this:

\newfontfamily{\rim}[Path=./fonts/, Scale=1.5]{Rat Infested Mailbox}

And the font name was RatInfestedMailbox.ttf and xelatex couldn't find the font. When I renamed the font to 'Rat Infested Mailbox.ttf' xelatex finds it and is happy. In fact, the name of the ttf file has to be exactly the same as the font name (including e.g. special character like & if they're in the font name and case sensitivity (on linux)).

So it looks like the search for the ttf file doesn't do anything clever at all (like scan all ttfs in the font paths looking for the right font name if it can't find a .ttf with the given name straight up).

Tags:

Xetex

Fontspec