Welcome to the UK List of
TeX Frequently Asked Questions
on the Web


The index of Frequently Asked Questions about TeX is searchable.

Please enter your keyword here:   then press here: ,    or:

Commands gobble following space

People are forever surprised that simple commands gobble the space after them: this is just the way it is. The effect arises from the way TeX works, and Lamport describes a solution (place a pair of braces after a command’s invocation) in the description of LaTeX syntax. Thus the requirement is in effect part of the definition of LaTeX.

These FAQs, for example, is written in LaTeX for production of a web site. The HTML code is generated by a script that requires a pair of braces, to make a null argument, as in:


for almost all macro invocations, regardless of whether the following space is required: however, these FAQs should not itself be regarded as a model of LaTeX style.

Many users find all those braces become very tedious very quickly, and would really rather not type them all.

An alternative structure, that doesn’t violate the design of LaTeX, is to say \fred\ — the \ command is “self terminating” (like \\) and you don’t need braces after it. Thus one can reduce to one the number of extra characters one needs to type.

If even that one character is too many, the package xspace defines a command \xspace that guesses whether there should have been a space after it, and if so introduces that space. So “fred\xspace jim” produces “fred jim”, while “fred\xspace. jim” produces “fred. jim”. Which usage would of course be completely pointless; but you can incorporate \xspace in your own macros:

and we find \restenergy available to us...

The \xspace command must be the last thing in your macro definition (as in the example); it’s not completely foolproof, but it copes with most obvious situations in running text.

The xspace package doesn’t save you anything if you use it in a macro that appears only once or twice within your document, and it is not totally foolproof. (The original author of the package wrote it because he had been bitten by lost spaces. He no longer recommends its use, simply because of the possibility of error.)

In any case, be careful with usage of \xspace — it changes your input syntax, which can be confusing, notably to a collaborating author (particularly if you create some commands which use it and some which don’t). No command built into LaTeX or into any “standard” class or package will use \xspace.

Distributed as part of macros/latex/required/tools (or browse the directory); catalogue entry

This answer last edited: 2013-11-26

Go to previous question, or next question

Go to FAQ home.

URL for this question: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=xspace

Comments, suggestions, or error reports? - see “how to improve the FAQ”.

This is FAQ version 3.28, released on 2014-06-10.