3

I try to have bm and siunitx packages transparent use based on this answer: bm and siunitx mismatch

\documentclass{article}
\usepackage{bm,siunitx}

\newif\ifbm
\bmfalse

% New bm
\let\Oldbm\bm
\renewcommand{\bm}[1]{%
    \bmtrue\Oldbm{#1}\bmfalse}

% New num
\let\Oldnum\num
\renewcommand{\num}[2][]{%
    \ifbm\Oldbm[math-rm=\mathbf,#1]{#2}%
    \else\Oldbm{#2}%
    \fi}

% New SI
\let\OldSI\SI
\renewcommand{\SI}[2][]{%
    \ifbm\OldSI[
            math-rm=\mathbf,
            number-unit-product={\mbox{\bfseries\,}},#1
            ]{#2}%
    \else\OldSI{#2}%
    \fi}

\begin{document}

$\bm{bob\num{5}\SI{30}{\km}}$
\end{document}

But I have this error message :

! Improper alphabetic constant.
<to be read again> 
\reserved@d 
l.25 $\bm{bob\num{5}}
                   $
2

There are errors in your code. E.g. you are using \Oldbm instead of \Oldnum in the \num definition. And \SI has three arguments not two.

A working code would be the following. Be aware that as both commands have optional arguments and so are fragile they need to be protected by a brace. So imho you don't gain much compared to https://tex.stackexchange.com/a/416405/2388

\documentclass{article}
\usepackage{bm,siunitx}

\newif\ifbm
\bmfalse

% New bm
\let\Oldbm\bm
\renewcommand{\bm}[1]{%
    \bmtrue\Oldbm{#1}\bmfalse}

% New num
\let\Oldnum\num
\renewcommand{\num}[2][]{%
    \ifbm {\Oldnum[math-rm=\mathbf,#1]{#2}}%
    \else  \Oldnum[#1]{#2}%
    \fi}

% New SI
\let\OldSI\SI
\renewcommand{\SI}[3][]{%
    \ifbm{\OldSI[
            math-rm=\mathbf,
            number-unit-product={\mbox{\bfseries\,}},#1
            ]{#2}{#3}}%
    \else\OldSI[#1]{#2}{#3}%
    \fi}

\begin{document}
\num{5} \SI{30}{\km}

$\bm{bob}$

$\bm{{\num{5}}}$

$\bm{bob{\num{5}\SI{30}{\km}}}$
\end{document}

enter image description here

| improve this answer | |
  • Thank you. Thank you for the corrections. But the 2 arguments new \SI command is based on egreg's 2 arguments answer. I agree with you, your protected solution is better than creating new commands. I accept your both answers. – Tarass Feb 21 '18 at 9:54
  • Well sometimes it works if you use less arguments, the commands can pick them up from the following stream. But I added braces (not quite sure if they are really needed) to protect the command and then this will fails. – Ulrike Fischer Feb 21 '18 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.