顯然 \IfFileExists 不適用於 otf?

顯然 \IfFileExists 不適用於 otf?

使用 TeXlive 2023、Linux、lualatex。

令我驚訝的是,\IfFileExists似乎沒有找到任何*.otf文件,無論該文件是否安裝到texmf-dist我的texmf主目錄中。微量元素:

% !TeX TS-program = lualatex
% !TeX encoding = UTF-8
\documentclass{article}
\usepackage{fontspec}
\IfFileExists{LibertinusSerif-Regular.otf}{
  \typeout{FOUND IT}
}{
  \typeout{NOT FOUND} % This is the response.
}
\setmainfont{Libertinus Serif}
\begin{document}
Hello World. % Prints in LibertinusSerif-Regular.otf.
\end{document}

當使用 進行編譯時lualatex,我希望命令列輸出(和日誌檔案)具有FOUND IT,因為該檔案肯定存在。如果它嘗試使用 Libertinus Serif 字體系列作為我的主要字體,它就可以工作。但\IfFileExists找不到該檔案及其正確的檔案副檔名。

我意識到有一個命令\IfFontExists,但如果字體不存在,可能需要很長時間,所以我不想這樣做。

答案1

\IfFileExists只能搜尋$TEXINPUTS樹,即有意義的文件\input(以及可能手動添加的內容)。

texmf-dist您可以使用 定義一個函數(幾乎)在整個樹中進行查找kpsewhich

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\IfFileInTEXTree}{O{}mmm}
 {
  \sys_get_shell:nnN { kpsewhich~#1~#2 } {\escapechar=-1\scan_stop:} \l_tmpa_tl
  \tl_if_blank:VTF \l_tmpa_tl { #4 } { #3 }
 }

\ExplSyntaxOff

\IfFileInTEXTree{LibertinusSerif-Regular.otf}{%
  \typeout{FOUND IT}%
}{
  \typeout{NOT FOUND}%
}

\IfFileInTEXTree{whatever.otf}{%
  \typeout{FOUND IT}%
}{
  \typeout{NOT FOUND}%
}

\stop

您也可以新增選項kpsewhich(請參閱其手冊)

\IfFileInTEXTree[<options>]{<filename>}{<true>}{<false>

這不需要無限制的 shell 轉義,只需要預設的受限 shell 轉義。

日誌檔案將顯示

(|kpsewhich  LibertinusSerif-Regular.otf)
FOUND IT
(|kpsewhich  whatever.otf)
NOT FOUND

相關內容