# When not to use \ensuremath for math macro?

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}

-
I won't answer, at least for one day, I promise. :) –  egreg Nov 14 '11 at 18:13
I know it's a close call, but tagging this question with mathmode seemed appropriate. ;-) –  lockstep Nov 14 '11 at 18:33
@hyh Do you mean \ensuremath{a+b=c} instead of $$a+b=c$$ or $a+b=c$? I'd consider it a perversion. ;-) –  egreg Feb 20 '13 at 7:29
@YiannisLazarides that has one problem: \mybeta\ldots gives a small space between the β and the ellipsis. Try moving the \xspace to the end of the command, i.e. after the \TextOrMath. I also recommend using $$ and $$ in macros for better error messages. –  kahen Nov 16 '11 at 5:16

This comment of user egreg to the question "Why are so many symbols restricted to math mode?" is very valuable:

If it allowed this, people would write \alpha+\beta getting the space wrong. Math should always treated as math, even single symbols; consider it as markup, if you want.

Put simply, use of \ensuremath can easily teach users of the macro in question to be lazy about explicitly switching to math mode (which is often "not needed" for all symbols to appear on the page if one has no super- or subscripts), with the consequence that spacing for an expression can be wrong if the expression should follow mathmode spacing but is typeset in horizontal textmode. This defeats one purpose of (La)TeX, namely correct typesetting of mathematical expressions.

If users can type "\myalpha+\mybeta" (or "\myalpha + \mybeta", and shouldn't it be "\myalpha{} + \mybeta{}" then? etc.) where they should be typing "$$\myalpha+\mybeta$$", that might lead to bad habits, with the spacing not being as it should.

-
I don't follow your claim that because \ensuremath might let some LaTeX users become lazy (in itself a debatable statement), the spacing in math expressions would somehow become incorrect. –  Mico Feb 25 '13 at 2:38
@Mico See my edit, which should clarify things. You've never seen questionable typesetting by "casual" users of LaTeX? ;-) Especially for spacing matters, a lot of casual users make mistakes or have no good sense of what good practice is. –  Lover of Structure Feb 25 '13 at 3:02