# Load fonts that are in a ./fonts directory

I'm new to TeX. I have the following problem: How can I load a font from a file in a directory, which is located in the root of the project?

Say I have something like:

/project
/fonts
DancingScript-Regular.ttf
main.tex
DancingScript-Regular.ttf


Loading it when it's right on the root seems to work.

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[a4paper,12pt]{book}
\usepackage{fontspec}
\setromanfont{DancingScript-Regular.ttf}
\begin{document}

hello world

\end{document}


However I can't figure how to load it when it's in the fonts directory. I've tried ./fonts/DancingScript-Regular.ttf, fonts/DancingScript-Regular.ttf, "fonts/DancingScript-Regular.ttf" and file:./fonts/DancingScript-Regular.ttf. None of them work.

All guides seem to focus on some sort of "how to install the font" but I can't work with that since the fonts may change (regularly) and would cause a sort of font version hell; it's also not very version control friendly. The (official?) docs don't seem to cover using your own fonts either.

Also, can I load the fonts as a sort of preset/alias? Since for a lot I have basically font families, so around 10 variants (oblique, bold, bold oblique, regular, etc). I would like to avoid repeating a declaration if possible.

-
@Martin this is a XeLaTeX question. TTF fonts are supported. –  Alan Munn Mar 3 '11 at 16:19
assuming you're on a *nix system, you might want to run the fc-cache command to update the fonts available to your system, then they will be available to xelatex without having to specify it. –  Mica Mar 3 '11 at 23:28
@Mica, my problem is cross-platform actually. I need a setup where people can collaborate on the project using something like git. –  srcspider Mar 3 '11 at 23:47

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.

-
ty, that worked perfectly :) –  srcspider Mar 3 '11 at 17:14
For anybody attempting this at home, the note "the trailing '/' is required" is not optional. I spent more time than I'd like to admit trying to get the Path correct until I realized that this comment in the code was the key. –  Hooked Feb 20 '13 at 18:52