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

I'm certain I have read that xspace can cause more problems than it solves. But I cannot find anything to that effect either on this site or the web. Are there any drawbacks or risks associated with it?

share|improve this question
Possibly outdated: groups.google.com/forum/?fromgroups=#!topic/comp.text.tex/… – lockstep Dec 11 '12 at 17:45
The example @lockstep refers to is \newcommand{\test}{\emph{test}\xspace} and still the problem exists: the italic correction is inserted in situations such as \test,, which is wrong. I never recommend (nor use myself) xspace. :) – egreg Dec 11 '12 at 17:59
@egreg: so you always insert spaces manually, or you do something clever? – Mohan Dec 11 '12 at 19:09
@Mohan I write \test{} if a space follows. That's all. – egreg Dec 11 '12 at 20:37
\newcommand{\newentity}[3][/]{\@ifdefinable{#2}{\def#2#1{#3}}} and then \newentity{\foo}{\emph{foo}} will define \foo to require / after it; or \newentity[|]{\bar}{\textbf{bar}} would require \bar|. As usual \makeatletter and \makeatother are necessary around the definition of \newentity; \newentity will throw an error if the command is already defined. – egreg Dec 11 '12 at 22:54
up vote 125 down vote accepted

I originally implemented xspace a long time ago (for LaTeX 2.09) as, like most LaTeX beginners, I'd had a document where I had omitted a {} or \ and so had a missing space in the final document.

The original version used a very simple nested test for following punctuation, but it could get the test wrong in some (well rather a lot really) of cases. Morten made the tests a lot better so the current version makes the correct choice in more cases and is more easily customised) however making the tests more complicated highlights the problem with this kind of package.

The rule in TeX is really quite simple, after a command name that uses letters (as opposed to single character command names using non-letters such as \$) white space is ignored. It is easy to forget to use \ or {} but the result of forgetting is very predictable, you lose white space in the result. Conversely with xspace the macro will get the correct space most of the time, but it isn't easy to predict when it will get it wrong, and so it's much harder to learn to enter the markup in a way that is always correct rather than having to always visually check for missing space, which rather negates the purpose of the command.

So, if you find it useful, fine, it's there. But personally I wouldn't recommend it.

share|improve this answer
Please add this discussion to the package documentation. – Martin Schröder Dec 18 '12 at 19:16
Are there any examples of erratic xspace behaviour? Such “wrong” cases are described neither in the xspace manual nor above. Could you post an example of xspace getting things wrong, please? – Andreï Kostyrka Feb 1 at 9:44
The answer in tex.stackexchange.com/a/15254/19642 is a rather obvious problem that is solved via adding an exception. Is there an example where xspace's incorrect spacing cannot be corrected using the package’s own capabilities? – Andreï Kostyrka Feb 1 at 9:53
@AndreïKostyrka oh basically it will always be possible to follow \xspace by a command that is sufficiently complicated that xspace can not work out if it will produce a space or not, you could make it call pgfmath rand and randomly add space for example but there are probably less silly examples. But even sensible examples if you need to check the output and then extend xspace's exception list it is probably safer just to always use \ to get the space when needed. – David Carlisle Feb 1 at 10:33

It has been mentioned on the site before, however, it seems to me that it can have its place here. You can define your commands in such a way that you can't forget the space at all. This is done by defining:


(you can't use \newcommand for this). This way, the proper syntax in-document is

The physisist \ES/ proved the so-called \ES/'s theorem.

You can test yourself that the space in \ES/ proved does not get eaten, because in real, the trailing / is not part of the name of the macro, but rather a sort-of "obligatory fixed-valued argument", that is called macro delimiter in TeX's jargon. If you omit this / and try to write \ES proved, you get a not-so-cryptic error message

Use of \ES doesn't match its definition

which means exactly what it says: You defined \ES it to be followed by / and this is missing.

However, the best thing to do is not to define shorthands like \ES for Einstein. Then you don't need xspace and you can't reach any such problems.

share|improve this answer
The Shorthand for Einstein is maybe superfluous but there are many cases where a shortcut comes in handy. E.g. if you want to format the name of something always in sans you should define a shortcut for \textsf{somename}. If your publisher then thinks that sans is bad simply change the definition instead of search for all occurrences. Furthermore defining such shortcuts ensures a consistent layout and fits (in my eyes) perfectly in the principles of logical markup … – Tobi Nov 26 '13 at 14:29
@Tobi Then, I would go with \N{Einstein} or a similar short-named command. – yo' Nov 26 '13 at 18:09
Sure if there are several names to be mentioned. But if you have only one name, like the name of a package, it should be a command. For example I use \newcommand{\metrix}{\textcolor{red}{\textbf{\textsf{m\u{e}trix}}}\xspace} in my metrix package, maybe even \LaTeX itself can be an example for that ;-) – Tobi Nov 26 '13 at 18:45
“… they are exceptional” depends on how you work and what you work on – I guess ;-) … I wasn’t aware of the xspace discussion yet, but I consider removing the \xspace :-) – Tobi Nov 26 '13 at 19:01
@Tobi: If you already mention... How \LateX is handling the issue? It seems like both \TeX and \LaTeX uses \@ at the end. Is this what takes care of the problem of space after the macro? – Dror Mar 26 '14 at 9:47

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.