SI inserts line break before unit
I'm not feeling at all comfortable with the way you are putting a whole display math and even normal text in a \bm
argument. Imho you should at least try to detect math mode.
\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}
This is similar to Ulrike’s solution, but more robust, see the final part.
\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}
By the way, \let\text\textbf
is wrong under many respects. The main one is the usual business described in the documentation of letltxmacro
. Here, however, there's a much more important aspect.
How does amstext
(that’s loaded by amsmath
and is responsible for defining \text
) makes \textbf
to change size when in subscripts or superscripts? Well, if \textbf{foo}
appears in math mode, LaTeX executes \nfss@text{\textbf{foo}}
and \nfss@text
is \mbox
when amstext
is not loaded. But amstext
does \let\nfss@text\text
: can you see the infinite loop? ;-)
Another "idiot solution", which simply uses text mode in all cases. This approach also simplifies the \sisetup
stuff.
\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}