The xspace package^{∗}

David Carlisle

1996/12/06

Abstract \xspace should be used at the end of a macro designed to be used mainly in text. It adds a space unless the macro is followed by certain punctuation characters.

1 Introduction

After \newcommand{\gb}{Great Britain\xspace}
\gb is a very nice place to live.

Great Britain is a very nice place to live.

\gb, a small island off the coast of France.

Great Britain, a small island off the coast of France.

\xspace saves the user from having to type \␣ or {} after most occurrences of a macro name in text. However if either of these constructions follows \xspace, a space is not added by \xspace. This means that it is safe to add \xspace to the end of an existing macro without making too many changes in your document.

Sometimes \xspace may make the wrong decision, and add a space when it is not required. In these cases follow the macro with {}, as this has the effect of suppressing the space.

Note that this package must be loaded after any language (or other) packages that make punctuation characters ‘active’.

2 The Macros

1 ⟨∗package⟩

\xspace \xspace just looks ahead, and then calls \@xspace.

 2 \DeclareRobustCommand\xspace{\futurelet\@let@token\@xspace}

\@xspace If the next token is one of a specified list of characters, do nothing, otherwise add a space. If you often use a different punctuation character, add the appropriate line (do not forget the \fi at the end!)

∗This file has version number v1.05, last revised 1996/12/06.

 3 \def\@xspace{%
 4 \ifx\@let@token\bgroup\else
 5 \ifx\@let@token\egroup\else
 6 \ifx\@let@token\/\else
 7 \ifx\@let@token\ \else
 8 \ifx\@let@token~\else
 9 \ifx\@let@token.\else
10 \ifx\@let@token!\else
11 \ifx\@let@token,\else
12 \ifx\@let@token:\else
13 \ifx\@let@token;\else
14 \ifx\@let@token?\else
15 \ifx\@let@token/\else
16 \ifx\@let@token'\else
17 \ifx\@let@token)\else
18 \ifx\@let@token-\else
19 \ifx\@let@token\@xobeysp\else
20 \ifx\@let@token\space\else
21 \space
22 \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

23 ⟨/package⟩