siunitx 无法检测半粗体字体

siunitx 无法检测半粗体字体

detect-all包的选项siunitx应该将周围文本的所有字体属性应用于数量和单位。但是,这似乎不适用于半粗体文本,请参见以下示例。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{MyriadPro}
\renewcommand{\bfdefault}{sb}
\usepackage[detect-all]{siunitx}

\begin{document}
\sffamily\bfseries \SI{230}{V} line voltage}
\end{document}

这只是一个错误吗?我该如何解决它?

答案1

今天我遇到了同样的问题。原因是权重检测依赖于系列的第一个字母,并且只检查b(粗体)和l(细体)。因此,所有其他权重都被忽略。我发布了一个GitHub 上的问题

一个不靠谱的解决方案是重新定义 的内部宏siunitx,这在 LaTeX3 惯例中实际上是“被禁止的”,并且对于未来的变化不可靠。下面是使用打开 Sans(自由的)fontspec访问所有权重。相关代码位于snip和之间snap

在此处输入图片描述

% !TeX program = xelatex
\documentclass[parskip=full]{scrartcl}

\usepackage{fontspec}
\setmainfont{Open Sans}[
   LetterSpace = 2,
   FontFace = {lt}{n}{* Light},
   FontFace = {sb}{n}{* Semibold},
   FontFace = {bf}{n}{* Bold},
   FontFace = {xb}{n}{* Extrabold},
   FontFace = {lt}{it}{* Light Italic},
   FontFace = {sb}{it}{* Semibold Italic},
   FontFace = {bf}{it}{* Bold Italic},
   FontFace = {xb}{it}{* Extrabold Italic},
]
\DeclareRobustCommand\ltseries{\fontseries{lt}\selectfont}
\DeclareTextFontCommand{\textlt}{\ltseries}
\DeclareRobustCommand\sbseries{\fontseries{sb}\selectfont}
\DeclareTextFontCommand{\textsb}{\sbseries}
\DeclareRobustCommand\xbseries{\fontseries{xb}\selectfont}
\DeclareTextFontCommand{\textxb}{\xbseries}

\usepackage{siunitx}
\sisetup{detect-all}

\ExplSyntaxOn\makeatletter

% --- snip ---
\cs_generate_variant:Nn \tl_if_in:nnTF { noTF }
\cs_set_protected:Npn \__siunitx_detect_font_weight_text: {
  \tl_if_in:noTF { sb bf xb } { \f@series }
    {
      \cs_set:Nn \__siunitx_font_weight: { \boldmath \use:c { \f@series series } }
    }
    {
      \cs_set:Nn \__siunitx_font_weight: { \use:c { \f@series series } }
    }
}
% --- snap ---

\makeatother\ExplSyntaxOff

\begin{document}
Upright shapes:

\textlt{Light: \SI{1.234e-5}{\micro\farad}}

Normal: \SI{1.234e-5}{\micro\farad}

\textsb{Semibold: \SI{1.234e-5}{\micro\farad}}

\textbf{Bold: \SI{1.234e-5}{\micro\farad}}

\textxb{Extrabold: \SI{1.234e-5}{\micro\farad}}


\bigskip
Italic shapes:

\itshape
\textlt{Light Italic: \SI{1.234e-5}{\micro\farad}}

Normal Italic: \SI{1.234e-5}{\micro\farad}

\textsb{Semibold Italic: \SI{1.234e-5}{\micro\farad}}

\textbf{Bold Italic: \SI{1.234e-5}{\micro\farad}}

\textxb{Extrabold Italic: \SI{1.234e-5}{\micro\farad}}
\end{document}

答案2

如果你愿意/能够使用fontspec使用 XeLaTeX 或 LuaLaTeX 和 OpenType 字体,它开箱即用,使用fontspec功能设置所需的粗体粗细,如下所示:

\setsansfont{Myriad Pro}[BoldFont=Myriad Pro Semibold]

完整代码:

\documentclass{article}
\usepackage{fontspec}
\setsansfont{Myriad Pro}[BoldFont=Myriad Pro Semibold]
\usepackage[detect-all]{siunitx}

\begin{document}\sffamily
\textbf{\SI{230}{\volt} line voltage}

{\bfseries \SI{230}{\volt} line voltage}

\SI{230}{\volt} line voltage
\end{document}

在此处输入图片描述

答案3

我在使用 LaTeX(不是 XeLaTeX)时遇到了同样的问题:siunitx(2018-05-27)与当前版本不兼容FiraSans(2019-06-06)。更准确地说,例如,使用“book”作为常规(中等)字体系列和/或使用“semibold”作为默认粗体字体系列会造成混淆,siunitx因为应该是常规的数字被设置为粗体,反之亦然。

根据 Tobi 的代码,我能够按如下方式解决该问题(相关部分位于% ==>和之间% <==):

% !TeX program = pdflatex


\documentclass[10pt, DIV=14]{scrartcl}


\usepackage[T1]{fontenc}

\usepackage[charter]{mathdesign}
\usepackage[scale=0.96]{XCharter}
\usepackage{FiraMono}
\usepackage[book, semibold, scale=0.92, lining, tabular]{FiraSans}
\DeclareRobustCommand{\lseries}{\fontseries{light}\selectfont}
\DeclareRobustCommand{\thseries}{\fontseries{thin}\selectfont}
\DeclareTextFontCommand{\textth}{\thseries}
\DeclareRobustCommand{\ulseries}{\fontseries{ultralight}\selectfont}
\DeclareTextFontCommand{\textul}{\ulseries}
\DeclareRobustCommand{\elseries}{\fontseries{extralight}\selectfont}
\DeclareTextFontCommand{\textel}{\elseries}
\DeclareRobustCommand{\ltseries}{\fontseries{light}\selectfont}
\DeclareTextFontCommand{\textlt}{\ltseries}
\DeclareRobustCommand\bkseries{\fontseries{book}\selectfont}
\DeclareTextFontCommand{\textbk}{\bkseries}
\DeclareRobustCommand\rgseries{\fontseries{regular}\selectfont}
\DeclareTextFontCommand{\textrg}{\rgseries}
\DeclareRobustCommand\meseries{\fontseries{medium}\selectfont}
\DeclareTextFontCommand{\textme}{\meseries}
\DeclareRobustCommand\sbseries{\fontseries{semibold}\selectfont}
\DeclareTextFontCommand{\textsb}{\sbseries}
\DeclareRobustCommand\bdseries{\fontseries{bold}\selectfont}
\DeclareTextFontCommand{\textbd}{\bdseries}
\DeclareRobustCommand\xbseries{\fontseries{extrabold}\selectfont}
\DeclareTextFontCommand{\textxb}{\xbseries}
\DeclareRobustCommand\hvseries{\fontseries{heavy}\selectfont}
\DeclareTextFontCommand{\texthv}{\hvseries}

\usepackage{siunitx}
\sisetup{detect-all}

% Fix incompatibility of siunitx (v2018-05-17) with FiraSans (v2019-06-06)
% ==>
\ExplSyntaxOn\makeatletter
\newcommand{\thisseries}{\f@series}
\newcommand{\thisshape}{\f@shape}
\cs_set_protected:Npn \__siunitx_detect_font_weight_text: {%
    \let\origmdseries\mdseries@sf%
    \let\origbfseries\bfseries@sf%
    \let\currentseries\f@series%
    \edef\XcurrentseriesX{/\f@series/}
        % Store the current fontseries but enclose it in some kind of delimiter,
        % because otherwise one-letter fontseries may erroneously triger \boldmath
        % (for instance, ``m'' is contained in ``semibold'')
    % Use \boldmath for any weight above semibold:
    \tl_if_in:noTF
        { /sb/ /b/ /bx/ /eb/ /ub/ /bold/ /extrabold/ /ultrabold/ /heavy/ /black/ /demibold/ /semibold/ }
        { \XcurrentseriesX }
        {% if included in the above list: switch to \mathversion{bold}
            \cs_set:Nn \__siunitx_font_weight: {%
                \boldmath%
                \let\bfseries@sf\currentseries%
                % necessary because siunitx in some way accesses
                % \bfseries@sf from the mweights package
                \fontseries{\bfseries@sf}\selectfont%
            }%
            \let\bfseries@sf\origbfseries%  % restore \bfseries@sf
        }
        {% if not: use \mathversion{normal}
            \cs_set:Nn \__siunitx_font_weight: {%
                \unboldmath
                \let\mdseries@sf\currentseries%
                % necessary because siunitx in some way accesses
                % \bfseries@sf from the mweights package
                \fontseries{\mdseries@sf}\selectfont%
            }%
            \let\mdseries@sf\origmdseries%  % restore \mdseries@sf
        }%
}
\makeatother\ExplSyntaxOff
% <==

\newcommand{\testSI}{%
    \begin{tabular}{@{} p{0.15\textwidth} *{3}{p{0.2\textwidth}} @{}}
        \thisseries/\thisshape &
        \SI{1.234e-5}{\micro\farad} &
        $\SI{1.234e-5}{\micro\farad}$ &
        $1.234 \times 10^{-5} \mathrm{\mu F}$
    \end{tabular}%
}


\begin{document}


\section*{Serif font (XCharter)}

\textmd{\testSI -- \texttt{\textbackslash textmd}} \\
\textbf{\testSI -- \texttt{\textbackslash textbf}} \\
\itshape
\textmd{\testSI -- \texttt{\textbackslash textmd}} \\
\textbf{\testSI -- \texttt{\textbackslash textbf}} \\
\upshape
\testSI -- back to default


\sffamily

\section*{Fira Sans upright shapes}

\textmd{\testSI -- \texttt{\textbackslash textmd}} \\
\textbf{\testSI -- \texttt{\textbackslash textbf}}

\smallskip
\noindent
\textth{\testSI} \\
\textul{\testSI} \\
\textel{\testSI} \\
\textlt{\testSI} \\
\textbk{\testSI} \\
\textrg{\testSI} \\
\textme{\testSI} \\
\textsb{\testSI} \\
\textbd{\testSI} \\
\textxb{\testSI} \\
\texthv{\testSI} \\
\testSI -- back to default


\section*{\textit{Fira Sans italic shapes}}

\itshape
\textmd{\testSI -- \texttt{\textbackslash textmd}} \\
\textbf{\testSI -- \texttt{\textbackslash textbf}}

\smallskip
\noindent
\textth{\testSI} \\
\textul{\testSI} \\
\textel{\testSI} \\
\textlt{\testSI} \\
\textbk{\testSI} \\
\textrg{\testSI} \\
\textme{\testSI} \\
\textsb{\testSI} \\
\textbd{\testSI} \\
\textxb{\testSI} \\
\texthv{\testSI} \\
\testSI -- back to default

\rmfamily\upshape
\section*{Serif font again}

\textmd{\testSI -- \texttt{\textbackslash textmd}} \\
\textbf{\testSI -- \texttt{\textbackslash textbf}} \\
\itshape
\textmd{\testSI -- \texttt{\textbackslash textmd}} \\
\textbf{\testSI -- \texttt{\textbackslash textbf}} \\
\upshape
\testSI -- back to default

\smallskip
\noindent
It works! :-)


\end{document}

以下是输出的屏幕截图:

FiraSans 和 siunitx

相关内容