# Acceptable use cases for xspace; When will it fail?

I have used xspace for a long time to take care of adding space after usage of some macros. Usually, it is with a macro to format a word consistently, as in:

\documentclass[]{article}
\usepackage{xspace}
\begin{document}
\newcommand{\matlab}{\textsc{matlab}\xspace}
\newcommand{\enterprise}{\emph{USS Enterprise\nocorr}\xspace}
For me, \matlab is the best; I like to program in \matlab.

The \enterprise was a famous ship.
\end{document}


In a recent question, I made the comment that

The xspace package is very nice to avoid the need for {} at every use of a macro defined in such a manner.

It was replied to me that

Many people say so, but the author of xspace isn't so sure: Drawbacks of xspace

The conclusion at the referenced post is "if you find it useful, fine, it's there. But personally I [David Carlisle, original author of xspace] wouldn't recommend it."

Yet, an answer not too long after by egreg seems to use xspace without hesitation, recommending it implicitly in the use of simple macros, e.g.:

\usepackage{xspace}

\newcommand{\Poincare}{Poincar\'e\xspace}
\newcommand{\adele}{ad\ele\xspace}
\newcommand{\Cech}{\v{C}ech\xspace}
\newcommand{\Erdos}{Erd\H{o}s\xspace}
\makeatletter
\newcommand{\etale}{\'etal\@ifstar{\'e}{e\xspace}}
\makeatother


So, my questions come down to these three:

1. Are there any acceptable (non-controversial) use cases for xspace? (perhaps the case as shown above from egreg, similar to my original example?)

2. What types of cases might the use of xspace actually cause problems? (Is it just things like the italic correction of the \enterprise example above?)

3. If problems in spacing are caused via a macro, will xspace fail in the same way with every use of that macro, or will it be dependent on the context where the macro is used?

• I'm not a fan of \xspace, but for those commands, which are likely to appear just in “quiet” places, it can be employed. The case of \emph{\Poincare} (where \xspace could fail to add the italic correction) might have been mentioned. May 27, 2014 at 17:56

As I comment in the referenced answer, part of the problem with xspace is that it's not entirely trivial to answer this question. That is xpsace fails when it fails and it isn't so easy to give a useful summary of exactly when that is.

xspace has to look ahead to decide whether to add a space or not. Basically it doesn't add space if it sees punctuation. But the macro seeing punctuation and punctuation appearing in the typeset result isn't the same thing.

so

\xspace,


the , is detected and no space is added.

\xspace\mbox{,}


The ,' isn't detected.

Now probably you don't type that in your document, but if you are using a babel shortcut for language sensitive punctuation, or a package for smart quotes or any one of a thousand other possibilities, then the effective result may be similar.

So basically \xspace works if the arbitrary tokens that follow the macro that ends with \xspace are defined in a way that allows \xspace to see any characters that ought to suppress the space.