[Tex/LaTex] How to load a texlive font with fontspec

fontsfontspeckpathsea

This is a XeLaTeX question. It seems really silly to ask after many years of using fontspec and XeLaTeX. I suppose I have always found a workaround. This time, I'd like a definitive answer.

Situation

A server (compiler/build system) is running in the background, generating documents (among other operations) from version control repositories around the clock. Documents are edited on several clients by humans. It cannot be assumed that the system fonts are the same on any of machines. There are two things that are the the same on all machines (or at least should be):
1. the TeX Live version/MikTeX. 2. The repository files.
This implies that any fonts I load should either be in:

  1. TeX Live build-in Fonts (assuming same as MikTeX)
  2. Fonts files in the repository

NOT system fonts as listed by fc-list -f "%{family}\n" | sort -u on unix-based machines.

Loading Fonts with fontspec

In fontspec, fonts are loaded several ways:

  1. File name with extension

e.g.

\setmainfont{texgyrepagella-regular.otf}
\let\texgyrepagellaregular\normalfont % create semantically relevant macro (name+font) to be consistent with other \newfontfamily macros
  1. Filename without extension but with path construction expressions given as parameters:

e.g.

\setmainfont{texgyrepagella}[
Path = /Users/will/Fonts/ ,
UprightFont = *-regular ,
BoldFont = *-bold ,]

I'd like to avoid loading fonts by typing /usr/local/texlive/<YEAR>/[…] because they may not be located in the same location.

Attempt

\documentclass{article}
\usepackage{fontspec}
\setmonofont{Inconsolata}
\newcommand\escapedlog[1]{\par\ttfamily #1\par}
\begin{document}
Keep it simple.
\escapedlog{Hello}
\end{document}

Log Output (error)

I think I know what is happening here. fontspec is checking the list of system fonts only fc-list -f "%{family}\n" | sort -u.

kpathsea: Running mktextfm inconsolata
mktextfm: Running mf-nowin -progname=mf \mode:=ljfour; mag:=1; nonstopmode; input inconsolata
This is METAFONT, Version 2.7182818 (TeX Live 2015) (preloaded base=mf)


kpathsea: Running mktexmf inconsolata
! I can't find file `inconsolata'.
<*> ...our; mag:=1; nonstopmode; input inconsolata

Please type another input file name
! Emergency stop.
<*> ...our; mag:=1; nonstopmode; input inconsolata

Transcript written on mfput.log.
grep: inconsolata.log: No such file or directory
mktextfm: `mf-nowin -progname=mf \mode:=ljfour; mag:=1; nonstopmode; input inconsolata' failed to make inconsolata.tfm.
kpathsea: Appending font creation commands to missfont.log.


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! fontspec error: "font-not-found"
!
! The font "inconsolata" cannot be found.
!
! See the fontspec documentation for further information.
!
! For immediate help type H <return>.
!...............................................

l.3 \setmonofont{inconsolata}

?

Traditional Packages

According to this list, I should have that font installed.

What font packages are installed in TeX live?

Best Answer

You don't need to give an explicit path, so long as the font is in a directory searched by XeTeX. The relevant (pseudo)environment variables are

% TrueType outline fonts.
TTFONTS = .;$TEXMF/fonts/{truetype,opentype}//;$OSFONTDIR//

% OpenType outline fonts.
OPENTYPEFONTS = .;$TEXMF/fonts/{opentype,truetype}//;$OSFONTDIR//

A user can set in the environment the variable OSFONTDIR, but setting OPENTYPEFONTS and TTFFONTS is a task performed by the kpse library, which xetex is linked to, so the user does not need to set them: they're automatically set as soon as xetex is started.

Depending on the operating system, you can make the system aware of the fonts in the current TeX distribution. As described in the installation guide of TeX Live, on most GNU/Linux systems you can run

cp $(kpsewhich -var-value TEXMFSYSVAR)/fonts/conf/texlive-fontconfig.conf /etc/fonts/conf.d/09-texlive.conf
fc-cache -fsv

with superuser privileges (sudo or similar) and the TeX Live OpenType and TrueType fonts will be available to the OS.

The first command should be loaded after the yearly TeX Live release, the second one should be run when updates or new fonts are installed with tlmgr. I do it on my GNU/Linux boxes.

Here's an example, where the \testpath family is only defined for crosschecking that the \testkpse suffices.

\documentclass{article}
\usepackage{fontspec}

\newfontfamily{\testkpse}{Inconsolatazi4}[
  Extension=.otf,
  UprightFont=*-Regular,
  BoldFont=*-Bold,
]

\newfontfamily{\testpath}{Inconsolatazi4}[
  Extension=.otf,
  Path=/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/,
  UprightFont=*-Regular,
  BoldFont=*-Bold,
]

\newfontfamily{\testfc}{Inconsolatazi4}

\begin{document}
\pagestyle{empty}

{\testkpse Inconsolata \bfseries Bold}

{\testpath Inconsolata \bfseries Bold}

{\testfc Inconsolata \bfseries Bold}

\end{document}

Running this example with

xelatex -output-driver="xdvipdfmx -vv" test

will show the following information on the terminal

<FONTMAP:/usr/local/texlive/2016/texmf-var/fonts/map/pdftex/updmap/pdftex.map><FONTMAP:/usr/local/texlive/2016/texmf-var/fonts/map/dvipdfmx/updmap/kanjix.map><FONTMAP:/usr/local/texlive/2016/texmf-dist/fonts/map/dvipdfmx/ckx.map>No dvi filename specified, reading standard input.
stdin -> incons.pdf
DVI Comment:  XeTeX output 2016.06.09:1437
<AGL:texglyphlist.txt><AGL:pdfglyphlist.txt><AGL:glyphlist.txt>[1</usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf@9.96pt<NATIVE-FONTMAP:/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf/0/H/65536/0/0>
pdf_font>> Input encoding "Identity-H" requires at least 2 bytes.
pdf_font>> The -m <00> option will be assumed for "/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf".
(CID:Inconsolatazi4-Regular)
pdf_font>> Type0 font "/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf" cmap_id=<Identity-H,0> opened at font_id=</usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf/0/H/65536/0/0,0>.
></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf@9.96pt<NATIVE-FONTMAP:/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf/0/H/65536/0/0>
pdf_font>> Input encoding "Identity-H" requires at least 2 bytes.
pdf_font>> The -m <00> option will be assumed for "/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf".
(CID:Inconsolatazi4-Bold)
pdf_font>> Type0 font "/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf" cmap_id=<Identity-H,0> opened at font_id=</usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf/0/H/65536/0/0,1>.
></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf@9.96pt></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf@9.96pt></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf@9.96pt></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf@9.96pt>]
otf_cmap>> Creating ToUnicode CMap for "/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf"...

otf_cmap>> Creating ToUnicode CMap for "/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf"...
(CID:/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Regular.otf[CIDFontType0])(CID:/usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/Inconsolatazi4-Bold.otf[CIDFontType0])

that shows the correct font is picked up in each case.

enter image description here