How to Use a Font in the Current Directory with luaotfload
At a glance
- 1. Use LuaLaTeX
Compile with LuaLaTeX, since fontspec and luaotfload do not work with pdfLaTeX.
- 2. Point to the file
Load the font by its filename, or set a path and filename pattern for a family of font files.
- 3. Check the build directory
Make sure the path is relative to the directory from which the TeX job runs.
To use a font file stored beside your LaTeX source, compile with LuaLaTeX and load it with fontspec. You can refer to the font by its filename, rather than by the family name registered with your operating system. This direct-file approach is usually the simplest way to test a font or keep a project’s font files together.
Load a font by filename
Here is a complete minimal example. Replace MyFont-Regular.otf with the actual filename, including its extension.
\documentclass{article}
\usepackage{fontspec}
\setmainfont{[./MyFont-Regular.otf]}
\begin{document}
This text uses the local font file.
\end{document}
Save the .tex file and font in the same directory, then compile the document with LuaLaTeX. The square brackets tell fontspec to interpret the value as a font filename; ./ means the current working directory. You can also use a relative subdirectory, such as [./fonts/MyFont-Regular.otf].
This example selects one font file for the main text. If you need bold or italic text, provide the corresponding font files as well; do not assume that LuaLaTeX will find other styles just because they share the same family name.
Set up a family with multiple styles
For a family whose files follow a consistent naming pattern, specify the directory and the style filenames. For example, if the files are Example-Regular.otf, Example-Bold.otf, Example-Italic.otf, and Example-BoldItalic.otf in the current directory, use:
\setmainfont{Example}[
Path = ./,
Extension = .otf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
BoldItalicFont = *-BoldItalic
]
Here Example is the filename stem, and * in each style option stands for that stem. If the files are in a folder called fonts, set Path = ./fonts/. Adjust the names and extension to match the files exactly. Font filenames and paths can be case-sensitive, depending on the operating system.
To set a local sans-serif or monospaced family instead, use \setsansfont or \setmonofont with the same filename or path options.
Which directory does ./ mean?
The relative path is normally interpreted from the working directory of the TeX process, not necessarily from the directory where the .tex file is saved. If you run LuaLaTeX after changing into the project directory, ./ points there. Some editors and build tools run TeX from a different directory, so a path that works in a terminal may fail in the editor.
If the font is in a project subfolder, use a path relative to the working directory, such as Path = ./fonts/. Alternatively, configure your editor or build command to use the project directory as its working directory. An absolute path can help diagnose a path problem, but a relative project path is generally easier to share with collaborators.
Do you need to update luaotfload’s database?
Usually not when you specify the font file directly. luaotfload can load the named file without first finding it by its installed family name. Updating the font database is relevant when you want to select a font by family name and have luaotfload discover it through its configured font search paths. For a portable document, keeping the font in the project and referring to its file is often more predictable.
Common mistakes
- Compiling with pdfLaTeX:
fontspecrequires LuaLaTeX or XeLaTeX. Select LuaLaTeX as the engine in your editor or build command. - Using the family name instead of a filename: A font that is not installed may not be discoverable by family name. Use the bracketed filename form for a local file.
- Leaving out the extension or misspelling the filename: Check the exact file name, including capitalization and
.otfor.ttf. - Assuming the path is relative to the source file: Check the build tool’s working directory if LuaLaTeX reports that it cannot find the font.
- Providing only a regular style: If the document requests bold or italic text, define those style files too, or use a font family that contains them.
A missing-file error is usually a path or filename issue; it does not by itself mean that the font needs to be installed system-wide or registered in luaotfload’s database.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.