TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I insert a short maths section in a sentence, it renders fine with a space each side as expected:

...returned $\mathcal{P}$ from...

If I define a command to do the same thing, it also renders with a space each side:

...returned $\pc$ from...

However, if I define a function with ensuremath, the space following the expression disappears:

...returned \pc from...

Why does ensuremath do this? Is there a way to make it more analogous to manually entering math mode?


share|improve this question
up vote 26 down vote accepted

This has nothing to do with \ensuremath. Spaces after macros are ignored independent of whether they contain math or not.

Of course, you can't simply add a space to the definition, since this will incorrectly add spaces before e.g. punctuation marks, where you don't want them. So there are two ways around this. One is to simply insert an overt space whenever you need on using \:

returned \pc\ from...

The other solution is to use the xspace package, which inserts a space automatically and takes care not to insert the space before punctuation.


Then you don't need to type the explicit space yourself.

Please note that there are various disadvantages to using both \ensuremath and xspace. See:

share|improve this answer
There's another option of course, and that's using some braces: \pc{} or {\pc}. – Joshua Taylor Jun 27 '14 at 21:02

I would recommend you not to use \ensuremath for such thing, or you'll be tempted to write Let \pc = 3 be given... instead of Let $\pc=3$ be given... (The first variant will always leed to incorrect spacing.)

You can simply say:


and use it as I suggested before:

Here, $\pc$ is ...

This way, it is most consistent, you are in control of things, and two dollars are not so much more to type. As well, you can do without the xspace which again makes you lose some control over your TeX code.

share|improve this answer

A simple workaround, could be to simple add {} after the macro or to ensure that the macro has an argument. I had the same problem with


but using


instead sorted things out.

I would have liked to use


but \vec was already taken, and I didnt want to redefine it.

share|improve this answer
This is a very short answer ;-) Welcome to TeX.SX! – Christian Hupfer Sep 26 '14 at 13:19
Thanks. I have received so much help here, I hope can help out in return... Just to keep karma on my side and all... :-) – Quantifeye Sep 26 '14 at 14:01

Another improvement could be to redefine the \ensuremath command itself with something like


This way you can define your own command


(I know this IS trivial, but I think it could be a useful suggestion for beginners, as it is now for me)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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