4

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

  • 5
    Can you not arrange that the font installs to ~/texmf/fonts rather than ~/texmf/tex/latex/ putting fonts in the tex/latex area rather messes up the concept of a standard TDS directory structure. – David Carlisle Apr 11 '13 at 11:12
  • 3
    You can use the file name: \setmainfont{LinLibertine_R.otf}. fonts declared in this way are found in the texmf tree if the font is in fonts/opentype (for miktex this isn't necessary as it searchs the texmf tree anyway). But you should be aware that xelatex doesn't like it if two versions of a font exist on a system: it can mix them up and then the glyphs are wrong. Imho it is better to ask your users to install fonts if needed. – Ulrike Fischer Apr 11 '13 at 11:47
  • Thanks for the welcome and the suggestions! I'm currently testing with the Ovo font now (easier name with no spaces compared to Droid Sans Mono). I've tried putting the Ovo.ttf it into /home/username/texmf/fonts directly, /home/username/texmf/fonts/truetype/ and /home/username/texmf/truetype/packagename/Ovo, but I still get the error "The font "Ovo" cannot be found." while compiling with XeLaTeX. – dragly Apr 11 '13 at 15:19
  • @dragly Have you any development about the problem? Did TeX Live 2013 behave better? – egreg Nov 2 '13 at 22:16
  • 1
    Any news with TL 2014? The questionn is lingering around the list of unanswered, and it is a pity. – Johannes_B Mar 31 '15 at 19:46
4

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

  • Good points. In my answer, I mentioned that I distribute a font with my package. But it is my own font (also OFL), so I can control revisions. – user103221 Mar 19 '17 at 14:38
  • "It is seldom senseful...", but somtimes it is. You already mentioned a few cases (font is not free). In my case it is all about self-containment in a large collaborative project: Users check out (from an svn repo) project X, which uses internal package Y with nonfree font Z. I want them to be able to compile X with having to fiddle around as little as even possible with their LaTeX distro. (We are talking about a dozen different distros on all kinds of operating systems.) – Daniel Mar 19 '17 at 21:49
4
+250

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.

  • For me it's all about self-containment, see my comment on Ulrike's answer below. But it is good news that fontspec now supports at least testing for a font! (I was about to offer another bounty for this question in search for a less hacky solution than provided by egreg.) – Daniel Mar 19 '17 at 21:54
  • @Daniel As I recall, a font can be placed in the same user directory where the *.tex master document is located. TeX looks there, before looking anywhere else. In other words, the font does not need to be installed anywhere. You might check that out (I don't think the behavior is specific to my installation, but I don't know). – user103221 Mar 19 '17 at 22:30
  • Yes, that works. However, I would prefer not to clutter the top-level folder with all those font files. Isn't there a reliable way to get the path to a specific package (or to "myself" within the package)? From there on, I could easily construct a path to the fonts. – Daniel Mar 20 '17 at 20:21
  • @Daniel Actually, I looked at that, months ago, when I was developing my own class. Searched high and low, including here. It seems that there are only three ways to find a font file: (a) It is in a place where kpathsea expects to find fonts, and mktxlsr has been run; (b) It is in the same folder as the main *.tex document; (c) It is in a known directory location, and can be requested by file name with path. The last of these is not practical for a distributed package. I looked for code that enabled a *.sty file to discover its own location, and report the path. Does not exist. – user103221 Mar 20 '17 at 21:52
  • Bounty awarded for destroying my hope for self-containment :-) No, seriously: Now I know I have been barking the wrong tree. With the new fontspec support for testing for a font I can at least provide helpful messages. – Daniel Mar 24 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.