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:



Text mode: \FunctionF

Math mode: $G(x) = x^3 - \FunctionF$
  • 32
    I won't answer, at least for one day, I promise. :) – egreg Nov 14 '11 at 18:13
  • 6
    I know it's a close call, but tagging this question with mathmode seemed appropriate. ;-) – lockstep Nov 14 '11 at 18:33
  • 7
    FWIW, in ConTeXt I have been campaigning to remove $ 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. – Aditya Nov 14 '11 at 18:36
  • Related Question: Wrap many math macros into \ensuremath. – Peter Grill Apr 19 '18 at 17:03

Two objections come to mind:

  • If you don't know you will be entering math mode, then you may write something "semantically" correct that breaks.

For example:

 \mathmacro[$x^2$] % The dollar signs *leave* math mode

What's going on here is that \mathmacro[$x^2$] expands to


which expands to (effectively)


and you can see that if you write it outside of math mode, the second branch is taken, so the first dollar sign brings you into math mode and the one at the front of $x^2$ takes you out of it, with the reverse operation happening afterwards. This gives an error.

Of course, you aren't supposed to do that, since \mathmacro is actually a "math macro usable in text mode", so you should think of the thing between the brackets of its argument as being in math mode. Alas, this confuses both the author and the text editor's syntax highlighting, since it is a nonstandard assumption.

Edit: I would define this macro as:


and use it as:

$x^2$ sub one: $\mathmacro[x^2]$.

This way, the parts that are math are clearly math.

  • In the unlikely event that you or some package sets \everymath, you will be very surprised when your apparently text-mode macros start to look different.

On the subject of semantics, though, the issue is clear: \ensuremath breaks the separation between math and text, which are two very different things. TeX even has the distinction built in: different fonts, different spacing rules, different parsing rules. You can probably construct a lot more counterexamples by exploiting these.

What I mean by this is that in the following situation:

 A sub one: \mathmacro

 \bfseries A sub one: \mathmacro

you may be surprised that the bold text does not extend to the contents of the apparently text-mode \mathmacro.

What I'm saying is not so much that \ensuremath actually breaks anything as that it violates your expectations to the point that it makes things harder rather than easier.

  • Can you add some more detail as to why the dollar signs leave math mode? Also, how should the \mathmacro be defined for this case. – Peter Grill Nov 14 '11 at 19:30
  • 1
    Also, not sure I understand the other comments: I am only using \ensuremath for something that always needs to be math. Perhaps you are referring to this question about what is the proper way to use ensuremath to define a macro useable in and out of math mode, but think that is a totally different issue than what you mean. – Peter Grill Nov 14 '11 at 19:34
  • @PeterGrill: Perhaps my edit makes my intent clearer? – Ryan Reich Nov 15 '11 at 0:54
  • 1
    But in both of those cases, I would like to see a definition of \mathmacro so you don't have those issues? In the first one I don't know how it should be specified so that it can be used in the manner you used it, and in the second one the only thing I can see is that if the user explicitly entered math mode the intent would be clearer, and the user would sill be surprised and perhaps realize a bit quicker why the bold text did not extend into the math. – Peter Grill Nov 15 '11 at 1:21
  • @PeterGrill: My point is that you can't specify it so that it can both be used "naked", without explicit $...$, and also consistently with the expected state of TeX with respect to math mode. I would define it without the \ensuremath and enter math mode explicitly with each use. In the second example, it's true that one might be surprised not to get bold math even with an explicit math shift, but then they would remember "oh, font changes don't affect math, ok". As written, they might not even realize it's a math issue for a while. – Ryan Reich Nov 15 '11 at 1:25

There's nothing bad about \ensuremath per se. However, if we examine this question of yours I see no advantage whatsoever in being able to say \FunctionF in both text mode and math mode, instead of always sticking to \(\FunctionF\) (or $\FunctionF$ as others might prefer). There are many other posts here on TeX.SX that do the same. Take this one: what's the advantage of being able to write the ring~\Z{n} instead of the ring~$\Z{n}$ when, maybe, one has also to write the ring $\Z{m}\times\Z{n}$, which is highly probable if one wants that command?

Another post has the following code:


This is bad programming, in my opinion, as what's inside \ensuremath is to be typeset in math mode; the "correct" code should be

\vcenter{\mathsurround=0pt \halign{\hfill##\hfill\cr

Other cases in which \ensuremath makes things more complicated than necessary are this one and this one.

  • Since you are doing this \ensuremath-only-when-really-needed campaign, let me ask here: what if I use only \ensuremath and no $'s? Will that still mess things up? I wrote quite a bit but haven't encounter problem with that strategy, I just want to know if it will eventually go wrong. – h__ Feb 20 '13 at 6:52
  • 4
    @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
  • I did this: \newcommand{\m}{\ensuremath} and on my editor whenever I hit m then tab I got \m{} appear and the caret is put between { and }. No messy $'s at all. – h__ Feb 20 '13 at 7:32
  • 3
    You forgot to mention \Z{n} $\times$ \Z{n} ;) – yo' Apr 30 '13 at 14:13

I think the answer is provided in fixltx2e, although for most cases I don't see it as a problem, from the documentation:

4.4.1   Notes on the implementation strategy

Pr/3400 made \@fnsymbol decide between text-mode and math-mode, which requires a certain level of robustness somewhere as the decision between text and math must be made at typesetting time and not when inside \protected@edef or similar commands. One way of dealing with this is to make sure the value seen by \@fnsymbol is a fully expanded number, which could be handled by code such as

  \expandafter{\the\csname c@#1\endcsname}}

This would be a good solution if everybody used the high level commands only by writing code like \fnsymbol{footnote}. Unfortunately many classes (including the standard classes) and packages use the internal forms directly as in \@fnsymbol\c@footnote so the easy solution of changing \fnsymbol would break code that had worked for the past 20 years.

Therefore the implementation here makes \@fnsymbol itself a non-robust command again and instead uses a new robust command \TextOrMath, which will take care of typesetting either the math or the text symbol. In order to do so, we face an age old problem and unsolvable problem in TeX: A reliable test for math mode that doesn’t destroy kerning. Fortunately this problem can be solved when using eTeX so if you use this as engine for your LaTeX format, as recommended by the LaTeX3 Project, you will get a fully functioning \TextOrMath command with no side effects. If you use regular TeX as engine for your LaTeX format then we have to choose between the lesser of two evils: 1) breaking ligatures and preventing kerning or 2) face the risk of choosing text-mode at the beginning of an alignment cell, which was suppodes to be math-mode. We have decided upon 1) as is costumary for regular robust commands in LaTeX.

  • 3
    I replaced the .dtx source with corresponding output, hope you don't mind. – Andrey Vihrov Nov 14 '11 at 22:00
  • @AndreyVihrov Thanks, is a very good idea, didn't think of it:) – Yiannis Lazarides Nov 14 '11 at 22:29
  • So does this mean that we should be using \TextOrMath macro? Could you add an example of how to use this? – Peter Grill Nov 15 '11 at 16:32
  • 1
    @PeterGrill IMHO yes. Example \newcommand{\mybeta}{\TextOrMath{$\beta$\xspace}{\beta}}. – Yiannis Lazarides Nov 16 '11 at 1:56
  • @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.

  • 1
    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
  • 1
    @Mico Maybe it isn't about encouraging laziness but, rather, about encouraging bad habits where this is not a matter of laziness. If somebody is coming from a word processor, not thinking in terms of modes is natural; thinking in terms of modes is alien. So if it more-or-less works without switching modes, you don't need to be lazy to get into the habit of not switching in LaTeX either. But I don't find the accusation of laziness a helpful or illuminating one. It chalks up to character defects what would be better understood in terms of word processor vs. LaTeX conceptual schemes. – cfr Sep 15 '15 at 22:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.