如何使用 unicode-math 設定 \boldmath 字體?

如何使用 unicode-math 設定 \boldmath 字體?

我正在嘗試在 Linux Libertine 中排版文檔,我也想在數學模式下使用該字體來表示字母數字字元。我正在使用unicode-math,而不是mathspec,因為我需要mathup明確指定字母表以與hepnames.我沒有找到一種方法來做到這一點mathspec。我使用的引擎是XeLaTeX。我最初將 TeX Gyre Pagella Math 指定為符號等的完整數學字體。

我發現了幾個問題,如下面的 MWE 所示:

  1. 開關\boldmath沒有任何作用。
  2. 數字無法正確呈現為粗體和斜體,只有字母。
  3. \mathbf(MWE 中的最後一個「A = 1」)是在 Pagella 中渲染的,而不是在 Libertine 中渲染的。

我已經嘗試添加/{latin,Latin,num}range聲明中,但沒有成功。我在這裡缺少什麼?

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont[%
    Ligatures=TeX,
    BoldFont=LinLibertine_RB.otf,
    ItalicFont=LinLibertine_RI.otf,
    BoldItalicFont=LinLibertine_RBI.otf]
    {LinLibertine_R.otf}

\setmathfont{texgyrepagella-math.otf}

\setmathfont[range=\mathup]{LinLibertine_R.otf}
\setmathfont[range=\mathbf]{LinLibertine_RB.otf}
\setmathfont[range=\mathit]{LinLibertine_RI.otf}
\setmathfont[range=\mathbfit]{LinLibertine_RBI.otf}

\begin{document}
    \begin{tabular}{lll}
        Text     & A = 1               & \textit{B = 2}\\
        Textbf   & \textbf{A = 1}      & \textbf{\textit{B = 2}}\\
        Math     & $A = 1$             & $\mathit{B = 2}$\\
        Boldmath & {\boldmath $A = 1$} & {\boldmath $\mathit{B = 2}$}\\
        Mathbf   & $\mathbf{A = 1}$    & $\mathbfit{B = 2}$\\
    \end{tabular}
\end{document}

在此輸入影像描述

不改變字體,一切看起來都正常:

\documentclass{minimal}

\begin{document}
    \begin{tabular}{lll}
        Text     & A = 1               & \textit{B = 2}\\
        Textbf   & \textbf{A = 1}      & \textbf{\textit{B = 2}}\\
        Math     & $A = 1$             & $\mathit{B = 2}$\\
        Boldmath & {\boldmath $A = 1$} & {\boldmath $\mathit{B = 2}$}\\
    \end{tabular}
\end{document}

在此輸入影像描述

答案1

我找到了一個適合我的解決方案。version=bold正如大衛建議的那樣,我開始玩弄,但情況一直在變化全部我的數學模式文字粗體。最後我從檔案名稱切換到系統字體名稱。 (我最初想避免這種情況,因為我正在幾台不同的機器上處理同一個文件。)使用系統字體名稱,可以\boldmath 立即自動工作。我還發現\mathbfup必須指定而不是\mathbf.

使用系統字體名稱的另一個問題是,由於某種原因,半粗體 Libertine 樣式預設載入為粗體。這就是BoldFont在下面的 MWE 中進行規範的原因。

該解決方案並不完美。您可以看到數學模式下的所有數字都是直立呈現的,即使在\mathit.另外,其中的數字\mathbfit不是粗體。不過所有字體都是正確的,對於我的目的來說已經足夠了。

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont[
    BoldFont=Linux Libertine Bold,
    BoldItalicFont=Linux Libertine Bold Italic]
    {Linux Libertine}

\setmathfont{TeX Gyre Pagella Math}

\setmathfont[range=\mathup,
    BoldFont=Linux Libertine Bold,
    BoldItalicFont=Linux Libertine Bold Italic]
    {Linux Libertine}
\setmathfont[range=\mathit,
    BoldFont=Linux Libertine Bold Italic]
    {Linux Libertine Italic}
\setmathfont[range=\mathbfup]{Linux Libertine Bold}
\setmathfont[range=\mathbfit]{Linux Libertine Bold Italic}

\begin{document}
    \begin{tabular}{lll}
        ~        &\ Regular             &\ Italic\\
        Text     &\ A = 1               &\ \textit{A = 1}\\
        Textbf   &\ \textbf{A = 1}      &\ \textbf{\textit{A = 1}}\\
        Math     &\ $A = 1$             &\ $\mathit{A = 1}$\\
        Boldmath &\ {\boldmath $A = 1$} &\ {\boldmath $\mathit{A = 1}$}\\
        Mathbf   &\ $\mathbf{A = 1}$    &\ $\mathbfit{A = 1}$\\
    \end{tabular}
\end{document}

在此輸入影像描述

答案2

tg85 指出他對自己的問題所提供的答案有兩個問題:

該解決方案並不完美。您可以看到數學模式下的所有數字都是直立呈現的,即使在\mathit.另外,其中的數字\mathbfit不是粗體。不過所有字體都是正確的,對於我的目的來說已經足夠了。

unicode 數學手動的(圖 7)指出,直立形狀實際上是專門為數字選擇的,因為它們「即使是斜體樣式,也應始終取自直立字體」。與此聲明一致,Unicode 似乎沒有為斜體數字定義插槽,但它為粗體數字、無襯線數字等定義了插槽。https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode#Mathematical_Alphanumeric_Symbols_block

不過,我可以理解,人們仍然希望由unicode 數學為了與原始的基於 LaTeX 的排版保持一致,其中\mathit{1}產生斜體數字 1。因此,受到啟發tg85 的代碼,我想出了一個解決這兩個問題的解決方案(也適用於 LuaLaTeX):

% !TEX program = xelatex


\documentclass{minimal}


\usepackage{ifthen}
\usepackage[svgnames]{xcolor}

\usepackage[
   mathit = sym, mathup = sym, mathbf = sym,
   math-style = ISO, bold-style = ISO
]{unicode-math}

\setmainfont{LinLibertine_R.otf}[
   ItalicFont = LinLibertine_RI.otf,
   BoldFont = LinLibertine_RB.otf,
   BoldItalicFont = LinLibertine_RBI.otf
]
\setmonofont{FiraMono-Medium.otf}[
   Scale = 0.81,
   Color = DarkBlue
]

\setmathfont{XITSMath-Regular.otf}[
   BoldFont = XITSMath-Bold.otf
]
\setmathfont{XITSMath-Regular.otf}[
   version = mathitVersion
]
\setmathfont{XITSMath-Regular.otf}[
   version = mathbfitVersion
]
\setmathfont{XITSMath-Bold.otf}[
   version = boldmathVersion
]

\setmathfont{LinLibertine_R.otf}[
   range = up/{num, latin, Latin, greek, Greek},
   BoldFont = LinLibertine_RB.otf
]
\setmathfont{LinLibertine_RI.otf}[
   range = it/{num, latin, Latin, greek, Greek},
   BoldFont = LinLibertine_RBI.otf,
   NFSSFamily = mathitalic
]
\setmathfont{LinLibertine_RBI.otf}[
   range = bfit/{latin, Latin, greek, Greek},
   NFSSFamily = mathbolditalic
]
\setmathfont{LinLibertine_RB.otf}[
   range = bfup/{num, latin, Latin, greek, Greek}
]

\newif \ifBoldMath \BoldMathfalse
\let \mathversionorig \mathversion
\renewcommand{\mathversion}[1]{%
   \ifthenelse{\equal{#1}{bold}}{%
      \BoldMathtrue%
   }{%
      \BoldMathfalse%
   }%
   \mathversionorig{#1}%
}

\AtBeginDocument{%
   \let \mathitorig \mathit%
   \renewcommand{\mathit}[1]{%
      \ifBoldMath%
         \textbf{%
            \SetSymbolFont{__um_fam1}{boldmathVersion}{TU}{mathbolditalic}{b}{it}%
            \SetSymbolFont{__um_fam2}{boldmathVersion}{TU}{mathbolditalic}{b}{it}%
            \mathversion{boldmathVersion}%
            \(\mathitorig{#1}\)%
         }
      \else%
         \textit{%
            \SetSymbolFont{__um_fam1}{mathitVersion}{TU}{mathitalic}{m}{it}%
            \mathversion{mathitVersion}%
            \(\mathitorig{#1}\)%
         }%
      \fi%
   }%
   \let \mathbfitorig \mathbfit%
   \renewcommand{\mathbfit}[1]{{%
      \textbf{%
          \SetSymbolFont{__um_fam1}{mathbfitVersion}{TU}{mathbolditalic}{b}{it}%
          \mathversion{mathbfitVersion}%
          \(\mathbfitorig{#1}\)%
      }%
   }}%
}

\newcommand{\testUnicodeMath}{%
   \bigskip
   \begin{tabular}{l @{\quad}l @{\quad} l @{\quad} l @{\quad} l}
       Row
       & Math version/alphabet
       & Regular
       & Upright
       & Italic \\[\medskipamount]
       1
       & Text
       & A = 1a
       & \textup{B = 2b}
       & \textit{C = 3c} \\
       2 &
       \texttt{\textbackslash textbf}
       & \textbf{A = 1a}
       & \textbf{\textup{B = 2b}}
       & \textbf{\textit{C = 3c}} \\
       3
       & \texttt{\textbackslash mathversion\{normal\}} (default)
       & $A = 1a + \beta \times \Gamma \int_{-\infty}^{\infty} \frac{\mathup{d}x}{2}$
       & $\mathup{B = 2b + \beta \times \Gamma}$
       & $\mathit{C = 3c + \beta \times \Gamma}$ \\
       4
       & \texttt{\textbackslash mathversion\{bold\}}/\texttt{\textbackslash boldmath}
       & \boldmath $A = 1a + \beta \times \Gamma \int_{-\infty}^{\infty} \frac{\mathup{d}x}{2}$
       & \boldmath $\mathup{B = 2b + \beta \times \Gamma}$
       & \boldmath $\mathit{C = 3c + \beta \times \Gamma}$ \\
       5
       & \texttt{\textbackslash mathbf}/\texttt{\textbackslash mathbfup}/\texttt{\textbackslash mathbfit}
       & $\mathbf{A = 1a + \beta \times \Gamma \int_{-\infty}^{\infty} \frac{\mathup{d}x}{2}}$
       & $\mathbfup{B = 2b + \beta \times \Gamma}$
       & $\mathbfit{C = 3c + \beta \times \Gamma}$ \\
   \end{tabular}%
   \bigskip
   \bigskip
}


\begin{document}

\testUnicodeMath

\textit{Check that everything works properly by invoking it a~second time:}

\testUnicodeMath

\textit{Check that everything works properly by invoking it a~third time:}

\testUnicodeMath

\end{document}

這會產生以下輸出 - 請注意,表第 3 行中的「3」現在是斜體,表第 4 行和第 5 行中的「3」是粗體斜體:

\mathit 和 \mathbfit 已修復

\mathversion{bold}另請注意/\boldmath\mathbf/ \mathbfup/之間的對比\mathbfit:前者還使所有運算符、積分等變為粗體,而後者僅影響字母和數字。

答案3

使用包 amsbsy 和 \pmb 似乎得到了粗體。

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{amsbsy}

\setmainfont[%
Ligatures=TeX,
BoldFont=LinLibertine_RB.otf,
ItalicFont=LinLibertine_RI.otf,
BoldItalicFont=LinLibertine_RBI.otf]
{LinLibertine_R.otf}

\setmathfont{texgyrepagella-math.otf}

\setmathfont[range=\mathup]{LinLibertine_R.otf}
\setmathfont[range=\mathbf]{LinLibertine_RB.otf}
\setmathfont[range=\mathit]{LinLibertine_RI.otf}
\setmathfont[range=\mathbfit]{LinLibertine_RBI.otf}

\begin{document}
\begin{tabular}{lll}
    Text     & A = 1               & \textit{B = 2}\\
    Textbf   & \textbf{A = 1}      & \textbf{\textit{B = 2}}\\
    Math     & $A = 1$             & $\mathit{B = 2}$\\
    Boldmath & { $\pmb{A = 1}$} & {$\pmb{\mathit{B = 2}}$}\\
    Mathbf   & $\mathbf{A = 1}$    & $\mathbfit{B = 2}$\\
\end{tabular}
\end{document}

在此輸入影像描述

答案4

xelatexlualatex

\documentclass{standalone}
\usepackage{hepparticles}
\usepackage{hepnames}
\usepackage[math-style=ISO]{unicode-math}
\usepackage{libertinus}
\usepackage{array}
\begin{document}

\tabcolsep=10pt
    \begin{tabular}{>{\ttfamily\textbackslash}lll}
    normalfont & Regular           &\itshape Italic\\
    textnormal & A = 1             & \textit{A = 1}\\
    textbf     & \textbf{A = 1}    & \textbf{\textit{A = 1}}\\
    mathup     & $\mathup{A} = 1$  & $A = 1$\\
    symbf      & $\symbfup{A = 1}$ & $\symbfit{A = 1}$\\
    mathbf     & $\mathbf{A = 1}$  & $\mathbfit{A = 1}$\\
\end{tabular}

\end{document}

在此輸入影像描述

相關內容