不确定发生了什么变化siunitx
,但出于某种原因
\let\text\textbf
导致换行前单位由\SI{<value>}{<unit>}
宏调用时输出外部数学模式。问题文本为蓝色:
我已经有这个代码好几年了,直到现在才注意到这个问题。
我只注意到\SI{}{}
宏存在这个问题。还有其他宏会出现类似的问题吗?
代码:
\documentclass{article}
\usepackage{xcolor}
\usepackage{bm}
\usepackage{siunitx}
%% https://tex.stackexchange.com/questions/64547/getting-bm-to-pass-thru-siunitx-macros
\sisetup{detect-weight, detect-display-math}
\sisetup{detect-inline-weight=math}
\newcommand\SetInBoldFont[2]{%
\begingroup
\let\text\textbf%
\bm{\textcolor{#1}{#2}}%
\endgroup
}
\def\SIText{\SI{1}{\kilo\meter}}
\begin{document}
In math mode things work fine:
\SetInBoldFont{red}{%
\[
\SIText \text{ where $\si{\kilo\meter}$ is kilometers}
\]
}
Inline math also works: $\SetInBoldFont{red}{\SIText}$
Outside of math mode things break:
\SetInBoldFont{blue}{\SIText}
\end{document}
答案1
这与 Ulrike 的解决方案类似,但更为强大,请参阅最后一部分。
\documentclass{article}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{xcolor}
\usepackage{siunitx}
%% https://tex.stackexchange.com/questions/64547/getting-bm-to-pass-thru-siunitx-macros
\sisetup{detect-weight, detect-display-math}
\sisetup{detect-inline-weight=math}
\newcommand\SetInBoldFont[2]{%
\begingroup
\ifmmode
\textcolor{#1}{\bm{{#2}}}%
\else
\renewcommand{\seriesdefault}{\bfdefault}%
\bfseries\boldmath\textcolor{#1}{#2}%
\fi
\endgroup
}
\def\SIText{\SI{1}{\kilo\meter}}
\begin{document}
In math mode things work fine:
\SetInBoldFont{red}{%
\[
\SIText \text{ where $\si{\kilo\meter}$ is kilometers}
\]
}
Inline math also works: $\SetInBoldFont{red}{\SIText}$
Outside of math mode things don't break:
\SetInBoldFont{blue}{\SIText}
\textit{\SetInBoldFont{blue}{This is in italics \SIText}}
\SetInBoldFont{blue}{\textit{This is in italics \SIText}}
\SetInBoldFont{blue}{\normalfont\itshape This is in italics \SIText}
\end{document}
顺便说一句,\let\text\textbf
在许多方面都是错误的。主要错误是 文档中描述的常见业务letltxmacro
。然而,这里还有一个更重要的方面。
amstext
(由 加载amsmath
并负责定义\text
)\textbf
在下标或上标中如何改变大小?好吧,如果\textbf{foo}
出现在数学模式中,则 LaTeX 执行\nfss@text{\textbf{foo}}
并且在没有加载时\nfss@text
是。但是:你能看到无限循环吗?\mbox
amstext
amstext
\let\nfss@text\text
;-)
答案2
我对你将整个显示数学甚至普通文本放入参数中的方式感到很不舒服\bm
。在我看来,你至少应该尝试检测数学模式。
\documentclass{article}
\usepackage{xcolor}
\usepackage{bm}
\usepackage{siunitx}
%% https://tex.stackexchange.com/questions/64547/getting-bm-to-pass-thru-siunitx-macros
\sisetup{detect-weight, detect-display-math}
\sisetup{detect-inline-weight=math}
\newcommand\SetInBoldFont[2]{%
\begingroup
\color{#1}%
\ifmmode
\bm{{#2}}% either additonal braces here or at use around fragile commands.
\else
\bfseries\mathversion{bold}#2
\fi
\endgroup
}
\def\SIText{\SI{1}{\kilo\meter}}
\begin{document}
In math mode things work fine:
\SetInBoldFont{red}{%
\[
\SIText \text{ where $\si{\kilo\meter}$ is kilometers}
\]
}
Inline math also works: $\SetInBoldFont{red}{\SIText} $
And Outside of math mode: \SetInBoldFont{blue}{\SIText}
\end{document}
答案3
另一个“愚蠢的解决方案”,即在所有情况下都使用文本模式。这种方法也简化了事情\sisetup
。
\documentclass{article}
\usepackage{xcolor,siunitx}
\newcommand\SetInBoldFont[2]{%
\begingroup
\sisetup{detect-weight}%
\textbf{\color{#1}#2}%
\endgroup}
\def\SIText{\SI{1}{\kilo\meter}}
\begin{document}
In display math things work fine:
\[\SetInBoldFont{red}{\SIText}\]
In inline math things work too:
$\SetInBoldFont{purple}{\SIText}$
In text mode things are also fine:
\SetInBoldFont{blue}{\SIText}
\end{document}