# How to check from LaTeX whether a pfb file exists?

I need to write a tex file which checks whether certain files can be found by kpathsea library. It is easy to check whether a sty or an fd file can be found:

 \IfFileExists{hyperref.sty}{Hyperref found}{Hyperref is not found}
\IfFileExists{t1stix.fd}{Stix FD found }{Stix FD is not found}


However, I want to know whether, say, stix-mathrm.pfb can be found. Of course, I can easily do this from command line, as

kpsewhich stix-mathrm.pfb


Well, I can use write18 to call this command from a TeX file, but I wonder if there is a simpler way to do it.

Update: Ok, I probably should explain better what do I want. I want to check whether an installation of TeX is good enough for acmart to work. I know how to check the existence of style files and their versions.

What sometimes happens is that a user has incomplete font installation: fd and and sty are present, but the fonts themselves are absent. I need to check whether this is the case.

Since the check must be run by a user, I cannot rearrange file locations, input path, or use write18 because many users just do not know how to enable it.

Ideally I want a tex file which produces a pdf that says: "You have a good enough hyperref, but you lack libertine fonts. Please install them from .."

Well, I guess I need to parse log file then :(

Update 2: . Silly me. kpsewhich is enabled in write18 by default in TeXLive and MikTeX. I thought it was not.

Sorry for asking a stupid question.

You can use catchfile, whose interface is simpler than \read. This doesn't require shell escape (works with the restricted shell), but might not work in MiKTeX (sorry, can't test).

\documentclass{article}
\usepackage{catchfile}

\makeatletter
\newcommand{\IfExistsInTeXTrees}[1]{%
\begingroup
\CatchFileDef{\temp}{"|kpsewhich #1"}{\endlinechar=\m@ne}%
\expandafter\endgroup
\ifx\temp\@empty
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
}
\makeatother

\begin{document}

\IfExistsInTeXTrees{stix-mathrm.pfb}{YES}{NO}

\IfExistsInTeXTrees{funny.pfb}{YES}{NO}

\end{document}


An expl3 implementation:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn

\prg_new_protected_conditional:Nnn \boris_if_exists_in_tex_trees:n { T,F,TF }
{
\tl_set_from_file:Nnn \l__boris_if_exists_temp_tl
{
\endlinechar=-1
}
{
"|kpsewhich~#1"
}
\tl_if_blank:VTF \l__boris_if_exists_temp_tl
{
\prg_return_false:
}
{
\prg_return_true:
}
}
\tl_new:N \l__boris_if_exists_temp_tl
\cs_new_eq:NN \IfExistsInTeXTreesTF \boris_if_exists_in_tex_trees:nTF
\cs_new_eq:NN \IfExistsInTeXTreesT \boris_if_exists_in_tex_trees:nT
\cs_new_eq:NN \IfExistsInTeXTreesF \boris_if_exists_in_tex_trees:nF

\ExplSyntaxOff

\begin{document}

\IfExistsInTeXTreesTF{stix-mathrm.pfb}{YES}{NO}

\IfExistsInTeXTreesT{stix-mathrm.pfb}{YES}

\IfExistsInTeXTreesTF{funny.pfb}{YES}{NO}

\IfExistsInTeXTreesF{funny.pfb}{NO}

\end{document}

• @Boris This requires nothing special from the user (I'm just unsure about MiKTeX). – egreg Sep 23 '17 at 22:45
• Is kpsewhich enabled by default in write18? I thought not... – Boris Sep 23 '17 at 22:47
• The programs that can be run in the restricted shell escape currently are bibtex, bibtex8, extractbb, gregorio, kpsewhich, makeindex, repstopdf and texosquery-jre8. Also the last one might be a candidate for your purpose, but requires a Java VM installed on the user's system. – egreg Sep 23 '17 at 22:56
• It works with miktex too (both examples). – Ulrike Fischer Sep 24 '17 at 11:14

If you want to avoid shell-escape to enable you to run kpsewhich an alternative might be to put the fonts in the tex inputpath so you can use IfFileExists

\documentclass{article}

\begin{document}

\IfFileExists{stix-mathrm.pfb}{\show\yes}{\show\no}

\end{document}


This says no with the default path

$pdflatex pp446 This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) (preloaded format=pdflatex) restricted \write18 enabled. entering extended mode (./pp446.tex LaTeX2e <2017-04-15> Babel <3.13> and hyphenation patterns for 84 language(s) loaded. (/usr/local/texlive/2017/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/local/texlive/2017/texmf-dist/tex/latex/base/size10.clo)) (./pp446.aux) > \no=undefined. \reserved@a ->\show \no l.5 ...xists{stix-mathrm.pfb}{\show\yes}{\show\no} ?  but yes if you adjust the paths to append the type1 font path to the standard input path $ TEXINPUTS=:\\$T1FONTS pdflatex pp446
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./pp446.tex
LaTeX2e <2017-04-15>
Babel <3.13> and hyphenation patterns for 84 language(s) loaded.
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/size10.clo)) (./pp446.aux)
> \yes=undefined.
\reserved@a ->\show \yes

l.5 ...xists{stix-mathrm.pfb}{\show\yes}{\show\no}

?
`