Previous: , Up: Definitions   [Contents][Index]


12.13 xspace package

Synopsis:

\usepackage{xspace}
  ...
\newcommand{...}{...\xspace}

The \xspace macro, when used at the end of a command, adds a space unless the command is followed by certain punctuation characters.

After a command name that uses letters (as opposed to single character command names using non-letters such as \$), TeX gobbles white space. Thus, in the first sentence below, the output has ‘Vermont’ placed snugly against the period, without any intervening space.

\newcommand{\VT}{Vermont}
Our college is in \VT .
The \VT{} summers are nice.

But because of the gobbling, the second sentence needs the dummy curly braces or else there would be no space separating ‘Vermont’ from ‘summers’. (Many authors instead instead use a backslash-space \ for this. See \(SPACE).)

The xspace package provides \xspace. It is for writing commands which are designed to be used mainly in text. It inserts a space after that command unless what immediately follows is in a list of exceptions. In this example, the dummy braces are not needed.

\newcommand{\VT}{Vermont\xspace}
Our college is in \VT .
\VT summers are nice.

The default exception list contains the characters ,.'/?;:!~-), the open curly brace and the backslash-space command discussed above, and the commands \footnote or \footnotemark. Add to that list as with \xspaceaddexceptions{\myfni \myfnii} and remove from that list as with \xspaceremoveexception{!}.

A comment: many experts prefer not to use \xspace. Putting it in a definition means that the command will usually get the spacing right. But it isn’t easy to predict when to enter dummy braces because \xspace will get it wrong, such as when it is followed by another command, and so \xspace can make editing material harder and more error-prone than instead always remembering the dummy braces.


Unofficial LaTeX2e reference manual