如何定义新的字体系列而不事先指定字体(而是使用默认字体)?

如何定义新的字体系列而不事先指定字体(而是使用默认字体)?

我想为我的班级设置一个语义字体系列\newfontfamily\COMPONENTFont{XXX}

该类的用户可以自由地为 COMPONENT 选择字体,例如字体 YYY:\renewfontfamily\COMPONENTFont{YYY}

但是,我想避免提及特定的字体 XXX,而是让 fontspec 加载默认字体,该字体可能是 Latin Modern Roman。

但是,使用以下 MWE 时,我会收到 LuaLaTex 和 XeLaTeX 的错误:

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}

% both seem to set "lmr"
% but it cannot be read by luaotfload because it is not in otf, ttf or ttc format
% adding a \setmainfont{Fira Code} works because the macro returns "FiraCode(0)"
\makeatletter
\newfontfamily\COMPONENTFont{\f@family}
%\newfontfamily\COMPONENTFont{\rmdefault}
\makeatother

\titleformat{name=\section}{\LARGE\COMPONENTFont\scshape}{}{0mm}{\thesection\ }

\begin{document}
\section{Section with cool font}

Paragraph with boring font

% users can modify this
%\setmainfont{Comic Sans}
%\renewfontfamily\COMPONENTFont{Chiller}

\end{document}
This is LuaHBTeX, Version 1.17.0 (TeX Live 2023/Arch Linux) 
 restricted system commands enabled.
(./mwe.tex
LaTeX2e <2022-11-01> patch level 1
 L3 programming layer <2023-02-22>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2022/07/02 v1.4n Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-luatex.def)))
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec-luatex.sty
(/usr/share/texmf-dist/tex/latex/base/fontenc.sty)
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
(/usr/share/texmf-dist/tex/latex/titlesec/titlesec.sty)
luaotfload | db : Reload initiated (formats: otf,ttf,ttc); reason: Font "lmr" not found.
luaotfload | resolve : sequence of 3 lookups yielded nothing appropriate.

! Package fontspec Error: The font "lmr" cannot be found.

For immediate help type H <return>.
 ...                                              
                                                  
l.9 \makeatother
              
? 
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023/Arch Linux) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
(./mwe.tex
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2023-02-22>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2022/07/02 v1.4n Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-xetex.def)))
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty
(/usr/share/texmf-dist/tex/latex/base/fontenc.sty)
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
(/usr/share/texmf-dist/tex/latex/titlesec/titlesec.sty)
kpathsea: Running mktextfm lmr
mktextfm: Running mf-nowin -progname=mf \mode:=ljfour; mag:=1; ; nonstopmode; input lmr
This is METAFONT, Version 2.71828182 (TeX Live 2023/Arch Linux) (preloaded base=mf)


kpathsea: Running mktexmf lmr
! I can't find file `lmr'.
<*> ...e:=ljfour; mag:=1; ; nonstopmode; input lmr
                                                  
Please type another input file name
! Emergency stop.
<*> ...e:=ljfour; mag:=1; ; nonstopmode; input lmr
                                                  
Transcript written on mfput.log.
grep: lmr.log: No such file or directory
mktextfm: `mf-nowin -progname=mf \mode:=ljfour; mag:=1; ; nonstopmode; input lmr' failed to make lmr.tfm.
kpathsea: Appending font creation commands to missfont.log.


! Package fontspec Error: The font "lmr" cannot be found.

For immediate help type H <return>.
 ...                                              
                                                  
l.11 \makeatother
                 
? 

答案1

在类或包文件中定义\COMPONENTFont为相同的意思,\normalfont并指示用户可以执行以下任一操作

\RenewDocumentCommand{\COMPONENTfont}{}{<some code>}

或者

\renewfontfamily{\COMPONENTFont}{<font name>}[<options>]

示例 1,无用户设置

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}

%%% ---------------------------------------------- %%%
%%% this would go in the class or package file
\NewDocumentCommand{\COMPONENTFont}{}{\normalfont}

\titleformat{name=\section}
  {\LARGE\COMPONENTFont\scshape}
  {}
  {0mm}
  {\thesection\ }

\titleformat{name=\section,numberless}
  {\LARGE\COMPONENTFont\scshape}
  {}
  {0mm}
  {}
%%% end of class or package code
%%% ---------------------------------------------- %%%

% User code in the document

\begin{document}

\section*{Numberless section}

Some text

\section{Section with cool font}

Paragraph with boring font

\end{document}

在此处输入图片描述

示例 2,使用\renewfontfamily\setmainfont

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}

%%% ---------------------------------------------- %%%
%%% this would go in the class or package file
\NewDocumentCommand{\COMPONENTFont}{}{\normalfont}

\titleformat{name=\section}
  {\LARGE\COMPONENTFont\scshape}
  {}
  {0mm}
  {\thesection\ }

\titleformat{name=\section,numberless}
  {\LARGE\COMPONENTFont\scshape}
  {}
  {0mm}
  {}
%%% end of class or package code
%%% ---------------------------------------------- %%%

% User code in the document

\setmainfont{Libertinus Serif}
\renewfontfamily{\COMPONENTFont}{Source Sans Pro}

\begin{document}

\section*{Numberless section}

Some text

\section{Section with cool font}

Paragraph with boring font

\end{document}

在此处输入图片描述

示例 3,使用\RenewDocumentCommand

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}

%%% ---------------------------------------------- %%%
%%% this would go in the class or package file
\NewDocumentCommand{\COMPONENTFont}{}{\normalfont}

\titleformat{name=\section}
  {\LARGE\COMPONENTFont\scshape}
  {}
  {0mm}
  {\thesection\ }

\titleformat{name=\section,numberless}
  {\LARGE\COMPONENTFont\scshape}
  {}
  {0mm}
  {}
%%% end of class or package code
%%% ---------------------------------------------- %%%

% User code in the document

\setmainfont{Libertinus Serif}
\RenewDocumentCommand{\COMPONENTFont}{}{\normalfont\bfseries}

\begin{document}

\section*{Numberless section}

Some text

\section{Section with cool font}

Paragraph with boring font

\end{document}

在此处输入图片描述

答案2

提供的 NFSS 系列名称\f@family不能直接传递给 fontspec,因为它不是字体文件名,即使是,它也缺少对许多字体来说很重要的字体功能。

\renewfontfamily不仅允许重新定义由\newfontfamilythough定义的家族,还允许重新定义任意命令,因此您可以将默认值直接定义为文档命令,例如使用

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}

% This picks up the currently active family and encoding
\makeatletter
\expanded {%
  \NewDocumentCommand \noexpand \COMPONENTFont {}{%
    \noexpand \fontfamily {\f@family}%
    \noexpand \fontencoding {\f@encoding}%
    \noexpand \selectfont
  }%
}
\makeatother

\titleformat{name=\section}{\LARGE\COMPONENTFont\scshape}{}{0mm}{\thesection\ }

\begin{document}
\section{Section with cool font}

Paragraph with boring font

% users can modify this
%\setmainfont{Comic Sans}
%\renewfontfamily\COMPONENTFont{Chiller}

\end{document}

相关内容