1

I have written a long document with a lot of equations that contain both \bm and \mathbf. However, now I would like to switch their behaviour. So whenever I have \mathbf{X} in the document, I would like to compile \bm{X} behaviour and vice versa. Is there a way to achieve this without manually modifying each occurence?

Edit: Things I have tried:

\let\temp\mathbf
\let\mathbf\bm
\let\bm\temp

and

\newcommand{\tmpbf}[1]{\mathbf{#1}}
\renewcommand{\mathbf}[1]{\bm{#1}}
\renewcommand{\bm}[1]{\tmpbf{#1}}

both of which did not seem to work and freeze the pdflatex compilation.

  • Most editors have a global search-and-replace function. It should be easy to perform a \bm{ -> \waitbm{, \mathbf{ -> \bm{, \waitbm{ -> \mathbf{ switcheroo. That keeps your commands as expected and drastically decreases the potential for confusion (for other readers and future you). – moewe May 31 at 13:05
  • The \let approach should actually appear to sort of work in many normal circumstances, but does not work properly, because the commands are actually implemented via protected helper commands internally (see an example of how badly things can go wrong at gist.github.com/moewew/c58ef0165e758887dc6dc2c0be88d23a). You'd have to use tex.stackexchange.com/q/88001/35864. But really it seems much, much safer, easier and less confusing to let your editor to a bit of search and replace. – moewe May 31 at 13:11
  • while egreg's answer is a correct answer to the question note that \bm and \mathbf have quite different syntax and allowed arguments, and swapping them at all only works if your document restricts the uses of \bm to cases where \mathrm will not error. – David Carlisle May 31 at 15:38
  • You are right @DavidCarlisle already changed my mind about it but it is interesting that it is possible to some extent. – canbo Jun 7 at 6:57
1

The \bm command is very peculiar and you have to look at its precise implementation.

\documentclass{article}
\usepackage{amsmath,bm}
\usepackage{letltxmacro}

\makeatletter
% make \bm the same as \mathbf
\LetLtxMacro\bm\mathbf
% regenerate \mathbf like bm.sty does
\DeclareRobustCommand\mathbf{\bm@general\boldmath\bm@boldtable\mv@bold\bm@command}
\protected@edef\mathbf#1{\mathbf{#1}}
\makeatother

\begin{document}

$\mathbf{a}+\bm{X}$

\end{document}

Of course this might break if bm.sty gets changed. It would be much better to use semantic commands instead of the generic \mathbf and \bm to begin with.

So I recommend to change all occurrences of \bm into \fooA and of \mathbf into \fooB (you will know what better names to use) and then add to your preamble

\newcommand{\fooA}[1]{\mathbf{#1}}
\newcommand{\fooB}[1]{\bm{#1}}
| improve this answer | |
  • I have learnt my lesson for the long run. I will invest time to generate some default notation macros but this saves me a lot of time in the short run. Thanks a million for the quick fix. – canbo May 31 at 13:40

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.