2

I am trying to use custom OpenType fonts (using LuaLaTeX and the fontspec package) in a LyX document under version control. The document is potentially accessed by others on different machines, so I decided to ship the .otf files in a subdirectory of the document's folder rather than relying on them being installed on the system. In other words, I just wanted to have the document in a compilable state as soon as it is checked out from version control without the need of installing the otf files.

This is how I wanted to add the fonts in the document's LaTeX Preamble:

\setmainfont{foo}[
    Path = mypath,
    UprightFont = foo-regular.otf,
    BoldFont = foo-bold.otf,
    ItalicFont = foo-it.otf,
    BoldItalicFont = foo-boldit.otf,
]

As long as mypath is an absolute path, everything works fine. As soon as I want to use a relative path here (because absolute paths don't make sense for files under version control) the fonts are not found any more.

Is there any solution to either (a) make a relative path work here or (b) somehow determine the absolute path of the current folder (not LyX's temporary folder where the document is actually compiled) and plug it in at mypath? Or is there any other obvious solution that I have overlooked?

simon
  • 141
  • did you try ./mypath ? – Elad Den Aug 08 '16 at 10:40
  • I did. Doesn't work. I think the problem is that LyX copies its files to a temporary directory for compiling them. But it doesn't know that it has to copy the font files there. So both path and ./mypath refer to said temporary directory, which does not contain the otf files (as LyX did not copy them there). – simon Aug 08 '16 at 11:42
  • This theme pops up quite regularly. Imho only the lyx developers can do something about it. It should be possible to add "required resources" to a lyx document which is copied along with other resources (it should be possible, after all graphics and bib seems to work). – Ulrike Fischer Aug 08 '16 at 14:13
  • @UlrikeFischer I could not find an option to add required files in a generic way. There is graphics and bibliographies, yes sure, and relative paths work perfectly there. And then there is "Insert -> File -> ...", but this also handles specific types of content only (text files, TeX files, child LyX files) if I understand it correctly. I could not find a generic way of saying "please also include file XY when copying files to your temporary folder". – simon Aug 08 '16 at 14:40
  • Well you could try the equivalent of %\input{font.otf}. Or \iffalse \input{font.otf} \fi. Lyx will probably not realize that the file isn't \input at all. – Ulrike Fischer Aug 08 '16 at 14:56
  • @UlrikeFischer Thanks for your suggestion! Can you elaborate a bit on it? Where would I put the \input, how would I use it, and why would a relative path work there? – simon Aug 08 '16 at 15:04
  • I'm not a lyx user. I have no idea how insert->file exactly works and if you can misuse it for this. But it seems logical that lyx has to copy such files to the tmp-directory too. – Ulrike Fischer Aug 08 '16 at 15:17
  • @UlrikeFischer Indeed, if I choose the way with \iffalse ... \fi at the beginning of my document, inserting a font file via "Insert -> File -> Child Document" in place of the ..., then the font file ends up in the temporary folder. However, some kind of name mangling happens along the way, so e.g. the font fonts/Calluna/exljbris-Calluna-Regular.otfends up as 25_home_simon_Documents_git-repo_fonts_Calluna_exljbris-Calluna-Regular.tex, where the 25 seems to be a counter. I have no idea how I could "unmangle" this name. At this point I think I will give up. Thanks for your help. – simon Aug 08 '16 at 15:38
  • another shot in the dark, did you try placing the file in the same directory as the *.lyx ? – Elad Den Aug 09 '16 at 07:16
  • @EladDen Thanks for your suggestion, unfortunately it does not work, either. The problem remains the same: LyX does not copy the otf files to its temporary directory, to which the relative path points during compile time. So again it does not find the font files. – simon Aug 09 '16 at 09:10
  • 1
    Suggestions: 1) try adding prepending your paths with \input@path (in some appropriate way), which is defined by LyX to refer to the original directory; 2) open a bug report at http://www.lyx.org/trac/wiki/BugTrackerHome and request the feature of taking arbitrary file dependencies into account – G.M. Aug 10 '16 at 16:20
  • 1
    @G.M. You are my hero! \input@path does the trick, thanks a lot! Here is what works (supposing the otf files are in a subdirectory called "fonts" under the original directory): Setting Path = \input@path , and then setting UprightFont = fonts/foo-regular.otf, etc., surrounding everything with \makeatletter and \makeatother – simon Aug 10 '16 at 16:56

1 Answers1

2

Here is how I could solve it (thanks to the help of G.M.). Suppose the otf files are in a subdirectory called "fonts" that lies under the directory that contains the main LyX file:

\setmainfont{foo}[
    Path           = \input@path,
    UprightFont    = fonts/foo-regular.otf,
    BoldFont       = fonts/foo-bold.otf,
    ItalicFont     = fonts/foo-it.otf,
    BoldItalicFont = fonts/foo-boldit.otf,
]

As G.M. explained, \input@path refers to the original directory (i.e. not the temporary directory where everything will be compiled).

Update: In my original solution, I had everything surrounded with \makeatletter and \makeatother. However, as G.M. also mentioned, LyX's LaTeX Preamble already provides this. So this is not necessary here and might indeed even cause trouble.

simon
  • 141
  • 1
    Note that the LyX preamble is already between \makeatletter and \makeatother. Calling \makeatother early may cause unrelated problems. – G.M. Aug 11 '16 at 10:34
  • @G.M. Thank you for pointing this out. I will update my answer accordingly. – simon Aug 11 '16 at 12:19