Load font from package directory using fontspec

Have a look at the "novel" document class. Might be in your TeXlive 2016 distro, or if not, CTAN. The class includes its own custom font, which installs in the appropriate place (fonts/opentype/novel/) when you install the package (which is actually a document class).

Works, when installed via tlmgr (distribution installer). Also works if downloaded as TDS and installed in a TEXMF-LOCAL directory. Note that in some cases, you might need to rebuild luatex-cache.

Note that the custom font is not loaded "from the package directory." Rather, the installed package places the font in a proper directory where fonts are supposed to be. Does it automatically.

When I began to build this document class, I envisioned doing exactly what you asked in the question: Put the font where the package *.sty file is located. But that does not easily work, and in fact is not necessary, since the package installer can also install a font.

EDIT: Recently, fontspec added the capability to detect whether or not a font can be found, and take different actions based on whether or not the font is there. So, it is possible to write a macro that requests font A, but use B if A is not found, or use C if neither A nor B, and finally halt with a message if none of them are found. See the recent fontspec documentation.


There are a number of things to consider here

  1. When loading fonts which are stored in the texmf-tree you should use file names and not font names, as font names don't work on all OS out of the box. So use \newfontfamily\listingsfont[Scale=0.85]{droidsansmono.ttf} instead of \newfontfamily\listingsfont[Scale=0.85]{Droid Sans Mono}.
  2. fonts belong in the fonts folder of a texmf tree — fonts\opentype for otf-fonts and fonts\truetype for a ttf. Even if you find a method to keep the font near to your package: a large system like a TeX distribuation can only work if everyone sticks to some standards.

  3. It is seldom senseful to distribute a font automatically along with a class or packages. There are exceptions when you have full controll over the font, if it is really special and usable only with the class, or if it is not free. But in the case of Droid Sans Mono there is already a package with this font an CTAN and adding a second version to the texmf tree can only give troubles. E.g. xelatex doesn't like it if there are more than one version of a font in the search path.

So if you fear that some of your users don't have a font Droid Sans Mono which is on CTAN: tell them to update their TeX-System or install the font manually from CTAN.

If you really need to distribute the font along with the package, tell the users to install them in the correct folders (if you distribute your package through CTAN they will take care of this).