我正在尝试使用 Xelatex 添加字体,但是,虽然 fontspec 确实找到了该字体,但它立即声明它不存在。
以下是字体定义输出:
Package fontspec Info: Font family 'SourceSansPro(0)' created for font
(fontspec) 'SourceSansPro' with options
(fontspec) [Ligatures=TeX,Path=./static/fonts/sourcesans/,Extension
(fontspec) =
(fontspec) .ttf,UprightFont=*-Regular,BoldFont=*-Bold,ItalicFont=*-Italic,BoldItalicFont=*-BoldItalic].
(fontspec)
(fontspec) This font family consists of the following NFSS
(fontspec) series/shapes:
(fontspec)
(fontspec) - 'normal' (m/n) with NFSS spec.:
(fontspec) <->"[./static/fonts/sourcesans/SourceSansPro-Regular.ttf]/OT:script=latn;language=dflt;mapping=tex-text;"
(fontspec) - 'small caps' (m/sc) with NFSS spec.:
(fontspec) <->"[./static/fonts/sourcesans/SourceSansPro-Regular.ttf]/OT:script=latn;language=dflt;+smcp;mapping=tex-text;"
(fontspec) - 'bold' (b/n) with NFSS spec.:
(fontspec) <->"[./static/fonts/sourcesans/SourceSansPro-Bold.ttf]/OT:script=latn;language=dflt;mapping=tex-text;"
(fontspec) - 'bold small caps' (b/sc) with NFSS spec.:
(fontspec) <->"[./static/fonts/sourcesans/SourceSansPro-Bold.ttf]/OT:script=latn;language=dflt;+smcp;mapping=tex-text;"
(fontspec) - 'italic' (m/it) with NFSS spec.:
(fontspec) <->"[./static/fonts/sourcesans/SourceSansPro-Italic.ttf]/OT:script=latn;language=dflt;mapping=tex-text;"
(fontspec) - 'italic small caps' (m/scit) with NFSS spec.:
(fontspec) - 'bold italic' (b/it) with NFSS spec.:
(fontspec) <->"[./static/fonts/sourcesans/SourceSansPro-BoldItalic.ttf]/OT:script=latn;language=dflt;mapping=tex-text;"
(fontspec) - 'bold italic small caps' (b/scit) with NFSS spec.:
但是当我使用时\setmainfont
我得到了这个:
! Package fontspec Error: The font "SourceSansPro" cannot be found.
For immediate help type H <return>.
...
l.47
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!
Package fontspec Info: Could not resolve font "SourceSansPro/BI" (it probably
(fontspec) doesn't exist).
Package fontspec Info: Could not resolve font "SourceSansPro/B" (it probably
(fontspec) doesn't exist).
Package fontspec Info: Could not resolve font "SourceSansPro/I" (it probably
(fontspec) doesn't exist).
! Package fontspec Error: The font "SourceSansPro" cannot be found.
For immediate help type H <return>.
真正让我困惑的是,它可以在其他字体上运行得很好。
以下是导致此问题的 .tex(字体从 Google 字体下载):
\usepackage{fontspec}
\setsansfont{SourceSansPro}[
Path=./static/fonts/sourcesans/,
Extension = .ttf,
UprightFont=*-Regular,
BoldFont=*-Bold,
ItalicFont=*-Italic,
BoldItalicFont=*-BoldItalic
]
\setromanfont{Brygada1918}[
Path=./static/fonts/brygada/,
Extension = .ttf,
UprightFont=*-Regular,
BoldFont=*-Bold,
ItalicFont=*-Italic,
BoldItalicFont=*-BoldItalic
]
\setmonofont{JetBrainsMono}[
Path=./static/fonts/jetbrains/,
Scale=0.85,
Extension = .ttf,
UprightFont=*-Regular,
BoldFont=*-Bold,
ItalicFont=*-Italic,
BoldItalicFont=*-BoldItalic
]
\setmainfont{SourceSansPro}
Brygada 和 JetBrains 都运行良好。
答案1
正如我在评论中提到的,问题在于,当您将 SourceSansPro 设置为 Sans 字体时,您提供了查找字体的信息,但将其设置为主字体时却没有提供。
我在评论中提到了三个选项:(1)将所有额外信息\setmainfont
也放在一起:
\setmainfont{SourceSansPro}[
Path=./static/fonts/sourcesans/,
Extension = .ttf,
UprightFont=*-Regular,
BoldFont=*-Bold,
ItalicFont=*-Italic,
BoldItalicFont=*-BoldItalic
]
或者 (2) 你可以省略该\setmainfont{SourceSansPro}
行,并指示 LaTeX 默认使用 sans 字体\renewcommand*{\familydefault}{\sfdefault}
。
或者 (3) 您可以省略该行,只需\sffamily
在文档开始时手动切换到 sans。
实际上还有其他我没有提到的选项,例如(4)设置该字体名称的默认功能,然后更简单地设置该字体,如下所示
\defaultfontfeatures[SourceSansPro]{
Path=./static/fonts/sourcesans/,
Extension = .ttf,
UprightFont=*-Regular,
BoldFont=*-Bold,
ItalicFont=*-Italic,
BoldItalicFont=*-BoldItalic
}
\setsansfont{SourceSansPro}
\setmainfont{SourceSansPro}
最后,您可以将该信息放入 SourceSansPro.fontspec 文件中,如fontspec 文档第 11 页。