# Defining macro script which acts according to mode; math or normal

I want to create a macro say BOLDMATH such that whenever I use this macro it should do following:

if in equation environment or inline math mode then it should return \mathbf{.} else it should return $\mathbf{.}$

I can use 2 macros but I want a compact one.

• \newcommand\BOLDMATH[1]{\ensuremath{\mathbf{#1}}} – Steven B. Segletes May 19 '20 at 0:57
• In general, there is tex primitive \ifmmode ... \else ... \fi and LaTeX2e command \TextOfMath{<text mode>}{<math mode>}, – muzimuzhi Z May 19 '20 at 1:23
• @StevenB.Segletes I was looking for shortcut to use mathbf instead of textbf. Although it solved my problem, but I completely agree with your point that source code should be longer for clarity. Thanks – ketan bapat May 19 '20 at 1:24
• @muzimuzhiZ I was unaware of what you mentioned. I looked and found it it very useful. Thanks. – ketan bapat May 19 '20 at 3:02

## 1 Answer

Generally, what you ask is frowned upon, because it removes the context in which the symbol is being used. Generally, it is better to have a longer source document, if it helps to explain the context of the particular usage.

Nonetheless, you asked for it. However, it is not a "best practice".

\documentclass{article}
\newcommand\BOLDMATH[1]{\ensuremath{\mathbf{#1}}}
\begin{document}
In text, \BOLDMATH{x}, and in math, $y=m\BOLDMATH{x}+b$.
\end{document}