7

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.

Olius
  • 171
  • 4
  • @StevenB.Segletes I mention that same question in the first line of mine. What I am asking for here is examples of places that warrant use of \ensuremath, since that question shows many reasons not to use it. – Olius Jun 05 '20 at 16:44
  • 6
    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. – David Carlisle Jun 05 '20 at 16:50
  • 1
    I never use it. I try quite hard not to get confused if something enters math mode or not. – Ulrike Fischer Jun 05 '20 at 16:52
  • 1
    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. – David Carlisle Jun 05 '20 at 16:56
  • @DavidCarlisle Interesting, that is kind of what I was trying to do with the \nms and \category definitions: so that to say "In here, \catC is a category which I represent with the letter 'C' " I write

    \nms{\catC}{\category{C}}.

    Would you place the \nms definitions I used in the preamble?

    – Olius Jun 05 '20 at 17:25
  • @Olius but it is unlikely that you only want \catC in that one group of expressions in one document so you end up copying the same block of definitions multiple places. It seems unlikely that you want \catC to have a different meaning elsewhere so they could be globally defined. In the case you do want different local definitions you can define global commands that switch in each set same as pagestyles that encapsulate named bunches of definitions which can be switched in with a single command – David Carlisle Jun 05 '20 at 17:29
  • 3
    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. – Henri Menke Jun 06 '20 at 04:24
  • @DavidCarlisle True, but previous exposure to more modern programming languages makes me want to say that the \catC used 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
  • @HenriMenke Can't that just be used as $\bohrmagneton$ when not in math mode? I don't have any experience with the SI packages. – Olius Jun 06 '20 at 23:21
  • 2
    @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. – Henri Menke Jun 07 '20 at 00:09
  • 1
    @HenriMenke To generalize that, I think \ensuremath would 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
  • That's an interesting way of putting it…I wouldn't know, as I almost exclusively use AMS \text instead of \mathrm. – Someone Jul 29 '23 at 21:08

0 Answers0