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.
\ensuremath, since that question shows many reasons not to use it. – Olius Jun 05 '20 at 16:44\nmsand\categorydefinitions: so that to say "In here,\catCis a category which I represent with the letter 'C' " I write\nms{\catC}{\category{C}}.Would you place the
– Olius Jun 05 '20 at 17:25\nmsdefinitions I used in the preamble?\DeclareSIUnit\bohrmagneton{\ensuremath{\mu_B}}because this might actually appear in both text and math. – Henri Menke Jun 06 '20 at 04:24\catCused in one proof might not be the same as the one used in another. Yes, they print the same, but semantically they're different. I guess this screams anxiety issues though, so you're probably more right than I am. – Olius Jun 06 '20 at 23:13$\bohrmagneton$when not in math mode? I don't have any experience with the SI packages. – Olius Jun 06 '20 at 23:21The magnetic moment is \SI{5.3}{\bohrmagneton}. Usually thesiunitxpackage 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\ensuremathis justified. – Henri Menke Jun 07 '20 at 00:09\ensuremathwould be good for anything that is semantically a piece of text but should be formally typeset as a mathematical expression, kind of the opposite to\mathrm. – Long Horn Jul 19 '23 at 15:31\textinstead of\mathrm. – Someone Jul 29 '23 at 21:08