
使用 lualatex、Linux、TeXlive 2023。
當fontspec
找不到請求的字體時,它會發出一條訊息,定義如下(使用 expl3):
\__fontspec_msg_new:nnn {font-not-found}
{
The font "#1" cannot be found.
}
{
A font might not be found for many reasons.\\
Check the spelling, where the font is installed etc. etc.\\\\
When in doubt, ask someone for help!
}
就我而言,對於自訂文件類,找不到字體的原因只有一個,以及解決該問題的具體說明。這是因為使用者對字體的選擇是有限的。所以我想重新定義上面的消息。在fontspec
加載之後,但在查找任何字體之前,我想要這樣的東西:
\ExplSyntaxOn
\__fontspec_msg_new:nnn {font-not-found}
{
The font "#1" cannot be found.
}
{
You need to install package "this-font-package".\\
If your system does not allow you to install packages,\\
then download the font package zip archive, unzip it,\\
and place the *.otf files in the same directory as the main document.
}
\ExplSyntaxOff
我沒想到它會起作用(它不起作用),但我嘗試對其進行破解,\__fontspec_msg_renew
但這也不起作用(顯然沒有 msg_renew 這樣的東西)。
毫無疑問 expl3 可以處理這個問題。各位高手,怎麼辦?
答案1
您可以用來\msg_set:nnnn
為訊息設定新文本,但正如註釋中提到的,您可能不應該這樣做,而是從您自己的類別或套件中發出錯誤。
\documentclass{article}
\usepackage{fontspec}
\ExplSyntaxOn
\msg_set:nnnn {fontspec} {font-not-found}
{
The~font~"#1"~cannot~be~found.
}
{
You~need~to~install~package~"this-font-package".\\
If~your~system~does~not~allow~you~to~install~packages,\\
then~download~the~font~package~zip~archive,~unzip~it,\\
and~place~the~*.otf~files~in~the~same~directory~as~the~main~document.
}
\ExplSyntaxOff
\setmainfont{foo.otf}
\begin{document}
\end{document}
如果您希望使用變更訊息,\AtBeginDocument
則需要定義 a\NewDocumentCommand
來執行此操作:
\ExplSyntaxOn
\NewDocumentCommand{\fixerror}{}{
\msg_set:nnnn {fontspec} {font-not-found}
{
The~font~"##1"~cannot~be~found.
}
{
You~need~to~install~package~"this-font-package".\\
If~your~system~does~not~allow~you~to~install~packages,\\
then~download~the~font~package~zip~archive,~unzip~it,\\
and~place~the~*.otf~files~in~the~same~directory~as~the~main~document.
}
}
\ExplSyntaxOff
\AtBeginDocument{\fixerror}