I have gotten into the habit of using \ensuremath
around math components in every macro that I define, so that I can use the same macro inside and outside of math mode. But egreg's comments in \DeclareMathOperator won't take arguments has me thinking that perhaps I should not be doing this all the time:
It's not a good idea, in my opinion, to add
\ensuremath
; why should it be? Disclaimer: this is part of my\ensuremath
-only-when-really-needed campaign. :) - egreg
A related comment in Force line break inside a \lim argument in align environment:
Why
\ensuremath
? Wouldn't$\scriptstyle#1$
be easier to read? - egreg
In this case it was required to use math mode as we were within a \makebox
(i.e., text mode). So, out of habit I used \ensuremath
but egreg was right that in this case using $...$
would have worked just as well and was certainly easier to read. But, even in this case I still prefer to use \ensuremath
so that it is more obvious that this can be used in math and text mode without having to think about \makebox
being in text mode.
So, is there really any harm in always using \ensuremath
, at least for any of my personal macros? I realize that there is a slight extra overhead in processing, but is there something else to consider?
If there are other considerations when they macros are coming packages that are intended for general purpose use, I would be interested in those as well. The most obvious one is that we may want to make sure that the user of the macro knows that this macro requires math mode, so require them to only use the macro in math mode.
Since this references egreg's campaign, I felt I should include a MWE for new users as per my MWE-as-often-as-possible campaign:
\documentclass{article}
\newcommand{\FunctionF}{\ensuremath{x^2}}%
\begin{document}
Text mode: \FunctionF
Math mode: $G(x) = x^3 - \FunctionF$
\end{document}
$
altogether and let everyone enter the math mode using\mathematics{...}
,\math{...}
or\m{...}
(all of which are essentially equivalent to LaTeX's\ensuremath{...}
. So, I am also interested in knowing if there is any harm in using the latter.\ensuremath
.