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

% Terminology

% #1: #2 -> #3

% Formatting for mathematical object types
% ...
% Some stuff in my document.
% ...
    We now have the following generic theorem I am using for this example:
        Let \(
        \) and~\(
        \) be functors between three categories.
        Then the \termino{composition} of the functors \funS~and~\funT\
        is a functor~\(
        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.
% ...
% Some more stuff in my document,
% fortunately not polluted by local commands.

[*] 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.

  • 5
    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
  • 1
    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
  • 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. Jun 5 '20 at 16:56
  • 2
    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
  • 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. Jun 7 '20 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.