将标题中的数学字体切换为无衬线字体

将标题中的数学字体切换为无衬线字体

我用Alegreya 的 opentype 版本包括数学和Alegreya Sans 的 opentype 版本用于标题和说明。有时我在图表和表格的说明中会有一些数学运算,我也希望用无衬线字体来表示。我知道我可以声明一个无衬线版本,unicode-math可以与 等一起使用symsf{...}。但由于我的一些数学运算“隐藏”在化合物命令中,这对我来说是一个非常糟糕的选择。所以我尝试定义两个数学命令并将它们添加到 和 中\rmfamily\sffamily以下 MWE 对我有用:

\documentclass{scrbook}
\usepackage{xpatch}
\usepackage{amsmath}
\usepackage{fontspec}
\setmainfont[
    %Path={Alegreya/},% add appropriate directory
    Extension = .otf,
    UprightFont = *-Regular,
    ItalicFont = *-Italic,
    BoldFont = *-Bold,
    BoldItalicFont = *-BoldItalic,
]{Alegreya}

\setsansfont[
    %Path={AlegreyaSans/},% add appropriate directory
    Extension = .otf,
    UprightFont = *-Regular,
    ItalicFont = *-Italic,
    BoldFont = *-Bold,
    BoldItalicFont = *-BoldItalic,
]{AlegreyaSans}

\usepackage{unicode-math}
\setmathfont{latinmodern-math.otf}
\newcommand{\rmmath}{
    \setmathfont{Alegreya-Regular.otf}[Numbers={Lining, Proportional}]
    \setmathfont{Alegreya-Italic.otf}[range=it/{latin,Latin,greek,Greek}]
    \setmathfont{Alegreya-Regular.otf}[range=up/{latin,Latin,greek,Greek,num,},Numbers={Lining, Proportional}]
    \setmathfont{Alegreya-BoldItalic.otf}[range=bfit/{latin,Latin,greek,Greek}]
    \setmathfont{Alegreya-Bold.otf}[range=bfup/{latin,Latin,greek,Greek,num},Numbers={Lining, Proportional}]
}

\newcommand{\sfmath}{
    \setmathfont{AlegreyaSans-Regular.otf}[Numbers={Lining, Proportional}]
    \setmathfont{AlegreyaSans-Italic.otf}[range=it/{latin,Latin,greek,Greek}]
    \setmathfont{AlegreyaSans-Regular.otf}[range=up/{latin,Latin,greek,Greek,num,},Numbers={Lining, Proportional}]
    \setmathfont{AlegreyaSans-BoldItalic.otf}[range=bfit/{latin,Latin,greek,Greek}]
    \setmathfont{AlegreyaSans-Bold.otf}[range=bfup/{latin,Latin,greek,Greek,num},Numbers={Lining, Proportional}]
}

\xapptocmd{\rmfamily}{\rmmath}{}{}
\xapptocmd{\sffamily}{\sfmath}{}{}

\usepackage{chemmacros}
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}

\begin{document}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\    
\sffamily
Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound    
\end{document}

当我多次使用开关时,就会出现问题。所以这个...

\begin{document}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\sffamily
Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\rmfamily% ---> fails at this point (! LaTeX Error: Too many symbol fonts declared.)
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound
\end{document}

... 和这个 ...

\begin{document}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\sffamily
Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\begin{figure}
%   \caption{Figure.}% ---> fails at this point (! LaTeX Error: Too many symbol fonts declared.)
\end{figure}
\end{document}

...或者仅此而已...

\begin{document}
\begin{figure}
    \caption{Figure.}% ---> fails at this point (! LaTeX Error: Symbol font `__um_fam1' not defined.)
\end{figure}
\end{document}

... 代码中给出的错误全部失败。此行为几个月前已经提到过

其他解决方案建议mathversionunicode-math。但这缺少range=我需要的斜体变量和直立数字的参数。我也试用了sansmath-package,但无法让它与我的字体配合使用。

我想知道这个问题是否真的没有解决方法或解决方案unicode-math。如果有人能对此提出一些建议,或者提出一个我不知道的完全不同的解决方案,我将不胜感激。

PS:我更喜欢 XeLaTeX 解决方案,因为 LuaLaTeX 在我的系统上非常慢。

答案1

A

使用标题文本中的标记,具体来说,\symsf{}

首先,\ch{A_{$x$}B_{$x-1$} * $n$ H2O}没有\symsf

标题中的衬线数学

颜色编码显示数学部分是衬线;其余部分是 sf 风格的标题文本。

Sans 版本,使用\symsf{}

无衬线字体

但代码非常繁琐:

\ch{A_{$\symsf{x}$}B_{$\symsf{x}-\symsf{1}$} * $\symsf{n}$ H2O}

unicode-math文档:5.1 Math 'style',第 12 页:“如果需要的字形不符合包选项[字体样式]的映射,则需要标记”)

平均能量损失

\documentclass{book}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}%[Colour=red]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Serif}[range=up/{Latin,Greek,num},Colour=blue]
\setmathfont{Noto Serif Italic}[range=it/{latin,greek},Colour=red]

%\setmathfontface\capmath{Asana Math}[Colour=violet]

\usepackage{chemmacros}
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}
\newcommand{\compoundb}{\ch{A_{$\symsf{x}$}B_{$\symsf{x}-\symsf{1}$} * $\symsf{n}$ H2O}}


\begin{document}
\chapter{}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \mbox{\compound}.

\sffamily
\noindent Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \mbox{\compound}.

\ttfamily
\noindent Text in tt with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \mbox{\compound}.


\begin{figure}
\begin{center}
xxx
\end{center}
\caption{\compound}
\end{figure}

\begin{figure}
\begin{center}
xxx
\end{center}
\caption{\compoundb}
\end{figure}

\end{document}

或者,将整体\ch置于数学模式,然后使用\mathsf似乎有效:

使用 mathsf

$\mathsf{\ch{A_{x}B_{x-1} * n H2O}}$

“似乎”是关键词:$\mathit{\ch{A_{x}B_{x-1} * n H2O}}$产生相同的结果。

平均能量损失

\documentclass{book}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}%[Colour=red]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Serif}[range=up/{Latin,Greek,num},Colour=blue]
\setmathfont{Noto Serif Italic}[range=it/{latin,greek},Colour=red]

%\setmathfontface\capmath{Asana Math}[Colour=violet]

\usepackage{chemmacros}
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}
\newcommand{\compoundb}{\ch{A_{$\symsf{x}$}B_{$\symsf{x}-\symsf{1}$} * $\symsf{n}$ H2O}}
\newcommand{\compoundc}{$\mathsf{\ch{A_{x}B_{x-1} * n H2O}}$}


\begin{document}
\chapter{}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \mbox{\compound}.

\sffamily
\noindent Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \mbox{\compound}.

\ttfamily
\noindent Text in tt with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \mbox{\compound}.


\begin{figure}
\begin{center}
xxx
\end{center}
\caption{\compound}
\end{figure}

\begin{figure}
\begin{center}
xxx with \textbackslash mathsf
\end{center}
\caption{\compoundc}
\end{figure}

\end{document}

C

对于这个特定的\ch,无数学模式也“有效”,结果与(B)相同。

\ch{A_{x}B_{x-1} * n H2O}

结论

根据ch,可能根本不需要数学模式解决方案。

这些\captionsetup{textfont={sf}}必须在处理的后期发生,或者覆盖某些事物,但在sym..{}替换发生之前。

这是\symbf

符号

=====

附录

解决方法

障碍在于,似乎数学和文本无法真正地(暂时)在chemformula包的\ch命令中逃脱(chemformula.sty,第 3568 行,版本 4.15(2017))。

因此解决方法是手动标记。

有多种方法可以实现这一点。

假设两种字体必须相交

相交字体

math sans upright (sfup) - 在图片中用红色标记 - 和文本 sffamily - 用蓝色标记 -

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}[Colour=blue]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Sans}[range=sfup/{latin,greek,num},Colour=red]

\usepackage{chemformula}%3.3 own manual; separate from chemmacros since Jul-2013
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}
\newcommand{\compoundsfup}{\ch{A_{$\symsfup{x}$}B_{$\symsfup{x}-\symsfup{1}$} * $\symsfup{n}$ H2O}}


\begin{document}

Inline text with \compound .
\vspace{24pt}

\begin{tabular}{l|ll}
Text & $\symsfup{symsfup}$ & normal \\
\hline \\
\rmfamily rmfamily & \rmfamily\compoundsfup & \rmfamily\compound \\
\sffamily sffamily & \sffamily\compoundsfup & \sffamily\compound\\
\ttfamily ttfamily & \ttfamily\compoundsfup & \ttfamily\compound \\
\end{tabular}
\end{document}

标记可以完全手动完成,但很耗时,并且必须维护两个单独的标记:

方法 1

手动标记

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}

\newcommand{\compoundsfup}{\ch{A_{$\symsfup{x}$}B_{$\symsfup{x}-\symsfup{1}$} * $\symsfup{n}$ H2O}}

代码

\documentclass{article}
\usepackage{regexpatch}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}[Colour=blue]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Sans}[range=sfup/{latin,greek,num},Colour=red]

\usepackage{chemformula}%3.3 own manual Jul-2013
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}
\newcommand{\compoundsfup}{\ch{A_{$\symsfup{x}$}B_{$\symsfup{x}-\symsfup{1}$} * $\symsfup{n}$ H2O}}

\begin{document}

Inline text with \compound .


\begin{figure}
\begin{center}
xxx 
\end{center}
\caption{\compoundsfup}
\end{figure}

Two separate markups.



\end{document}

方法 2

稍微更有效的方法是解析源代码文本两次,使用正则表达式在一种情况下自动替换与之间的任何内容$...$,在另一种情况下:symbfupsymrm

解析标记

正则表达式为: = 之后的一个或多个非\$([^\$]+)\$的任何内容,直到下一个,视为一个组。$$$

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}[Colour=blue]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Sans}[range=sfup/{latin,greek,num},Colour=red]

\usepackage{chemformula}%3.3 own manual Jul-2013
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}



\ExplSyntaxOn
\NewDocumentCommand{\cmathrm}{m}
 {
  \tl_set:Nn \l_lyssane_cmathrm_tl { #1 }
  % change every run of anything between $ ...$ into $sym...{}$
  \regex_replace_all:nnN
   { \$([^\$]+)\$ }
   { \c{ensuremath}\cB\{\c{symrm}\cB\{\1\cE\}\cE\} }
   \l_lyssane_cmathrm_tl
  % print the result
  \tl_use:N \l_lyssane_cmathrm_tl
 }
\NewDocumentCommand{\cmathsf}{m}
 {
  \tl_set:Nn \l_lyssane_cmathsf_tl { #1 }
  % change every run of anything between $ ...$ into $sym...{}$
  \regex_replace_all:nnN
   { \$([^\$]+)\$ }
   { \c{ensuremath}\cB\{\c{symsfup}\cB\{\1\cE\}\cE\} }
   \l_lyssane_cmathsf_tl
  % print the result
  \tl_use:N \l_lyssane_cmathsf_tl
 }
\ExplSyntaxOff

\newcommand\compound{\ch{\cmathrm{A_{$2x$}B_{$x$-$1$} * $n$ H2O}}: roman h1.}
\newcommand\compoundcap{\ch{\cmathsf{A_{$2x$}B_{$x$-$1$} * $n$ H2O}}: sf h2.}


\begin{document}


{\rmfamily\compound} ; 

{\sffamily\compoundcap}


\begin{figure}
\begin{center}
xxx 
\end{center}
\caption{\compoundcap}
\end{figure}

\end{document}

(致谢:l3 代码改编自 egreg 的赫梯语音译解决方案)

\ch不知何故,在 之后丢失了一个空格n$n$很容易重新插入一个。(编辑添加:原因是解析器正则表达式步骤替换$n$ H\ensuremath{symsf{n}}H,因此\ch永远没有机会添加数学空格。哎呀!)

此方法要求提供两次输入代码,每个解析器一次。

\newcommand\compound{\ch{\cmathrm{A_{$2x$}B_{$x$-$1$} * $n$ H2O}}: roman h1.}

\newcommand\compoundcap{\ch{\cmathsf{A_{$2x$}B_{$x$-$1$} * $n$ H2O}}: sf h2.}

方法 3

并扩大$..$对的范围以包括,__以便可以独立于排版解析的文本\ch

\cmathrm{A$_{2x}$B$_{x-1}$ * $n$ H2O} i2a;

\cmathsf{A$_{2x}$B$_{x-1}$ * $n$ H2O} i2b.

表明\ch看到的是 SF 文本(蓝色),而不是 SF 数学(红色)。

更清晰的数学标记

这没问题,因为无论如何现在这都是科幻小说,并且反过来,标题中的科幻小说也出来了。

\documentclass{article}
\usepackage{regexpatch}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}[Colour=blue]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Sans}[range=sfup/{latin,greek,num},Colour=red]

\usepackage{chemformula}%3.3 own manual Jul-2013
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}




\ExplSyntaxOn
\NewDocumentCommand{\cmathrm}{m}
 {
  \tl_set:Nn \l_lyssane_cmathrm_tl { #1 }
  % change every run of anything between $ ...$ into $sym...{}$
  \regex_replace_all:nnN
   { \$([^\$]+)\$ }
   { \c{ensuremath}\cB\{\c{symrm}\cB\{\1\cE\}\cE\} }
   \l_lyssane_cmathrm_tl

  % print the result
  \tl_use:N \l_lyssane_cmathrm_tl
 }
\NewDocumentCommand{\cmathsf}{m}
 {
  \tl_set:Nn \l_lyssane_cmathsf_tl { #1 }
  % change every run of anything between $ ...$ into $sym...{}$
  \regex_replace_all:nnN
   { \$([^\$]+)\$ }
   { \c{ensuremath}\cB\{\c{symsfup}\cB\{\1\cE\}\cE\} }
   \l_lyssane_cmathsf_tl

  % print the result
  \tl_use:N \l_lyssane_cmathsf_tl
 }
\ExplSyntaxOff




\begin{document}


\begin{figure}
\begin{center}
xxx i2
\end{center}
\caption{\ch{\cmathsf{A$_{2x}$B$_{x-1}$ * $n$\ H2O}} i2b.}
\end{figure}

Normal text, without \textbackslash ch:

\cmathrm{A$_{2x}$B$_{x-1}$ * $n$  H2O} i2a;

\cmathsf{A$_{2x}$B$_{x-1}$ * $n$  H2O} i2b.
\vspace{24pt}

rmfamily and sffamily, with \textbackslash ch:

{\rmfamily\ch{\cmathrm{A$_{2x}$B$_{x-1}$ * $n$ H2O}} i2a rmf};

{\sffamily\ch{\cmathsf{A$_{2x}$B$_{x-1}$ * $n$ H2O}} i2b sff}. {\small <= needs a space}




\end{document}

方法 4(不工作)

将输入代码放入一个宏需要适当抑制扩展(或转义代码)的级别,以防止解析器\ch获取已经标记的列表。

扩展宏

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{XITS Math}


\setmainfont{Noto Serif}
\setsansfont{Noto Sans}[Colour=blue]
\setmonofont{Noto Sans Mono}

\setmathfont{Noto Sans}[range=sfup/{latin,greek,num},Colour=red]

%\usepackage{chemmacros}
\usepackage{chemformula}%3.3 own manual Jul-2013
\usepackage{caption}
\captionsetup{
    labelfont={sf,bf},
    textfont={sf},
}

\newcommand\compoundi{A$_{2x}$B$_{x-1}$ * $n$ H2O}
%\edef\compoundia{\meaning\compoundi}



\ExplSyntaxOn
\NewDocumentCommand{\cmathrm}{m}
 {
  \tl_set:Nn \l_lyssane_cmathrm_tl { #1 }
  % change every run of anything between $ ...$ into $sym...{}$
  \regex_replace_all:nnN
   { \$([^\$]+)\$ }
   { \c{ensuremath}\cB\{\c{symrm}\cB\{\1\cE\}\cE\} }
   \l_lyssane_cmathrm_tl

  % print the result
  \tl_use:N \l_lyssane_cmathrm_tl
 }
\NewDocumentCommand{\cmathsf}{m}
 {
  \tl_set:Nn \l_lyssane_cmathsf_tl { #1 }
  % change every run of anything between $ ...$ into $sym...{}$
  \regex_replace_all:nnN
   { \$([^\$]+)\$ }
   { \c{ensuremath}\cB\{\c{symsfup}\cB\{\1\cE\}\cE\} }
   \l_lyssane_cmathsf_tl

  % print the result
  \tl_use:N \l_lyssane_cmathsf_tl
 }
\ExplSyntaxOff




\begin{document}

%\begin{figure}
%\begin{center}
%xxx f
%\end{center}
%\caption{\ch{\cmathb{$\mathsf{A}_{|2x|}\mathsf{B}_{|x|-|1|}$ * |n| H2O}} f}
%\end{figure}

\compoundi cmpd i -- the macro ; 

{\rmfamily\ch{\compoundi} cmpd i rm -- with \textbackslash ch }; 

{\sffamily\ch{\compoundi} cmpd i sf -- with \textbackslash ch }.

{\rmfamily\ch{\cmathrm{\compoundi}} cmpd i rm cm -- and parser}; 

{\sffamily\ch{\cmathsf{\compoundi}} cmpd i sf cm -- and parser}.

\end{document}

相关内容