[Tex/LaTex] Load font from package directory using fontspec

fontspecpackage-writingpackages

I'm working on a LaTeX package (in fact it is a document class) that I want to make available for others to install in their texmf directory. In this package I'm setting up a default configuration for the \lstset command with a given font. If I for instance would like to use Droid Sans Mono as the font, the following command works if the font is installed on the system:

\newfontfamily\listingsfont[Scale=0.85]{Droid Sans Mono}
\lstset {
    basicstyle=\footnotesize\listingsfont
}

However, if the font is not installed on the system, this would of course fail. Because of this, I would like to distribute the font together with the LaTeX package. I have tested the Path option of fontspec

\newfontfamily\listingsfont[Path=/home/username/texmf/tex/latex/mypackage/,Scale=0.85]{Droid Sans Mono}

which works perfectly fine, except that I would have to know the username beforehand.

Is there a way to specifiy paths relative to my package in fontspec? Or to get the path to the home directory?

(Most users of this package will use Ubuntu as their main OS).

Best Answer

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).