How do I load a texlive font with fontspec?
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/[email protected]<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/[email protected]<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/[email protected]></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/[email protected]></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/[email protected]></usr/local/texlive/2016/texmf-dist/fonts/opentype/public/inconsolata/[email protected]>]
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.
Use
\setmonofont{Inconsolatazi4}
or
\setmonofont[
BoldFont=Inconsolatazi4-Bold.otf]{Inconsolatazi4-Regular.otf}
By default fontconfig
, which is used by xetex/xelatex
for searching font files do not scan the TeX font directories. But xetex/xelatex
use kpsewhich
, the reason why it will find fonts with the full name. One can put these files into the systems font directoty or extend the search path for fontconfig
. Then one can use the symbolic name.