# When is \ensuremath really needed?

A rather popular question is When not to use \ensuremath for math macro?. Some good answers there explain why \ensuremath is often overused. egreg has an "\ensuremath-only-when-really-needed campaign".

So my question is: When is \ensuremath really needed?

For example, I myself tend to make (probably overzealous) use of macros with local scope,* and the way I do it results in a lot \ensuremath in my LaTeX. My excuse is that the macros have a small scope, and that it saves "some typing" I guess. I save not 1 but 2 characters by using \funS~and instead of $\funS$~and!

Of course this doesn't seem to be a place \ensuremath is really needed—although I like my setup—so I'm asking for evidence that egreg's campaign is "\ensuremath-only-when-really-needed" and not "\ensuremath-only-never-at-all" :)

% ...
% New Math Symbol
\newcommand*{\nms}[1]{\ensuremath{#1}}

% Terminology
\newcommand*{\termino}[1]{\emph{#1}}

% #1: #2 -> #3
\newcommand*{\morphism}[3]{#1\colon#2\to#3}

% Formatting for mathematical object types
\newcommand*{\category}[1]{\mathcal{#1}}
\newcommand*{\functor}[1]{#1}
% ...
\begin{document}
% Some stuff in my document.
% ...
{
\nms{\catA}{\category{A}}
\nms{\catB}{\category{B}}
\nms{\catC}{\category{C}}
\nms{\funS}{\functor{S}}
\nms{\funT}{\functor{T}}
We now have the following generic theorem I am using for this example:
\begin{theorem}
Let $$\morphism{\funS}{\catA}{\catB}$$ and~$$\morphism{\funT}{\catB}{\catC}$$ be functors between three categories.
Then the \termino{composition} of the functors \funS~and~\funT\
is a functor~$$\morphism{\funT\funS}{\catA}{\catC}$$.
\end{theorem}
\begin{proof}
The nonsense proof of the dull theorem above begins.
The important part about it is that we can use
the local commands referring to the mathematical objects
we are manipulating in this context,
for example \catC\ or~\funT.
\end{proof}
}
% ...
% Some more stuff in my document,
% fortunately not polluted by local commands.
\end{document}


[*] Why do I see so few people using local macros? Is it bad practice? Maybe I should post a separate question about it as it's kind of off topic.

• never really. It is used in some legacy commands but really for new commands it's best not to use it, define the command to be math or text. Jun 5 '20 at 16:50
• I never use it. I try quite hard not to get confused if something enters math mode or not. Jun 5 '20 at 16:52
• local macros is possibly not a good practice. A good aim is to separate out programming aspects (to the packages and if you must, the preamble) from a purely declarative document markup in the body. Jun 5 '20 at 16:56
• The only time I've ever (properly) used it was for \DeclareSIUnit\bohrmagneton{\ensuremath{\mu_B}} because this might actually appear in both text and math. Jun 6 '20 at 4:24
• @Olius You'd use it like The magnetic moment is \SI{5.3}{\bohrmagneton}. Usually the siunitx package will adapt the font of the unit to the surrounding context, i.e. in text mode it will use the upright text font and in math mode it will use the upright math font. In the case of \mu_B, however, I don't want either and I just want it to be typeset in math mode always and that is one rare instance where using \ensuremath is justified. Jun 7 '20 at 0:09