# How can I automatically boldify formulas that include text?

I have some simple formulas that I would like to have in bold and non-bold versions. Because retyping a formula is error-prone, I'd like to have a macro that takes the non-bold formula (which is wrapped in a macro) and emits a copy of the macro, but bolded.

What I have so far: (MNWE):

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\thispagestyle{empty}

\newcommand{\foo}{A\text{a}\text{b}+}
\newcommand{\baz}{B\text{c}\text{d}-}

\makeatletter
\newcommand{\boldify}[2]{%
\let\@boldme#1
\patchcmd{\@boldme}{\text}{\textbf}{}{}
\newcommand{#2}{\ensuremath{\boldsymbol{\@boldme}}}
}
\makeatother

\boldify{\foo}{\bffoo}
\boldify{\baz}{\bfbaz}

\begin{document}

$$\foo \bffoo$$
$$\baz \bfbaz$$

\end{document}


This produces:

There are three reasons I'm unsatisfied:

1. The second \text is not bolded.
2. The \text blocks in the last invocation replace those in the first.
3. I have to manually specify the name of the bold version of the command.

It's only the 2nd problem which is really critical for me.

What am I missing?

-

I am not sure I fully understand the question, but the \boldify macro below will replace all \text with \textbf:

## Notes:

• You should not use $$ with LaTeX as per Why is $...$ preferable to$$ ... ?
• I am not sure what the meaning of the trailing + and - signs is, but if they are representing a binary operator the spacing you have is incorrect. You can achieve the correct binary operator spacing with:

\newcommand{\foo}{A\text{a}\text{b}+{}}
\newcommand{\baz}{B\text{c}\text{d}-{}}


## Code:

\documentclass[12pt]{article}
\usepackage{amsmath}
\thispagestyle{empty}

\newcommand{\foo}{A\text{a}\text{b}+}
\newcommand{\baz}{B\text{c}\text{d}-}

\newcommand{\boldify}[1]{%
\let\text\textbf%
#1%
}

\begin{document}

$\foo \boldify{\foo}$

$\baz \boldify{\baz}$

\end{document}

-

\text isn't the right macro here, for it doesn't ensure that the contents are in upright shape: for instance in a theorem statement the contents will appear in italics.

\documentclass[12pt]{article}
\usepackage{amsmath}

\newcommand{\foo}{A\mathrm{a}\mathrm{b}+}
\newcommand{\baz}{B\mathrm{b}\mathrm{d}-}

\makeatletter
\newcommand{\boldify}[2]{%
\newcommand#2{\text{\boldmath$#1$}}%
}
\makeatother

\boldify{\foo}{\bffoo}
\boldify{\baz}{\bfbaz}

\begin{document}

\begin{gather*}
\foo \bffoo \\
\baz \bfbaz
\end{gather*}

\end{document}


If you only want to boldify the letters, then this will work:

\documentclass[12pt]{article}
\usepackage{amsmath,bm}

\makeatletter
\DeclareRobustCommand{\rmorbf}{%
\ifboldify
\expandafter\mathbf
\else
\expandafter\mathrm
\fi
}
\DeclareRobustCommand{\mdorbm}{%
\ifboldify
\expandafter\bm
\else
\expandafter\@firstofone
\fi
}
\newif\ifboldify
\newcommand{\boldify}[2]{%
\newcommand#2{\begingroup\boldifytrue#1\endgroup}%
}
\makeatother

\newcommand{\foo}{\mdorbm{A}\rmorbf{a}\rmorbf{b}+}
\newcommand{\baz}{\mdorbm{B}\rmorbf{b}\rmorbf{d}-}

\boldify{\foo}{\bffoo}
\boldify{\baz}{\bfbaz}

\begin{document}

\begin{gather*}
\foo \bffoo \\
\baz \bfbaz
\end{gather*}

\end{document}


-

I think the key to my problem was, as @egreg pointed out, a misuse of \text. Replacing this with \mathrm makes my macros respond to math bold commands. Given that, I don't think I need explicit bold macros any more.

MWE:

\documentclass[12pt]{article}
\usepackage{amsmath}
\thispagestyle{empty}

\newcommand{\foo}{A\mathrm{a}\mathrm{b}+}
\newcommand{\baz}{B\mathrm{c}\mathrm{d}-}

\begin{document}

$\foo \boldsymbol{\foo}$
$\baz \boldsymbol{\baz}$

\end{document}

-