13

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.

Boris
  • 38,129
  • 1
    See this answer https://tex.stackexchange.com/a/179748/2388 and also this https://tex.stackexchange.com/questions/306987/problem-with-writing-the-output-of-kpsewhich-to-a-file. – Ulrike Fischer Sep 23 '17 at 21:37

2 Answers2

8

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}

? 
David Carlisle
  • 757,742
7

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 } { \sys_get_shell:nnN { kpsewhich~#1 } { \endlinechar=-1 } \l__boris_if_exists_temp_tl \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}

egreg
  • 1,121,712