summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-xspace.html
blob: 8a9722a3adf18d2c7d27ffa9dcb60330bc691a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<head>
<title>UK TeX FAQ -- question label xspace</title>
</head><body>
<h3>Commands gobble following space</h3>
<p/>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&#8217;s invocation) in the description of LaTeX syntax.
Thus the requirement is in effect part of the definition of LaTeX.
<p/>This FAQ,
for example, is written with definitions that <em>require</em> one to
type <code>\</code><code>fred{}</code> 
                              
for almost all macro invocations, regardless
of whether the following space is required: however, this FAQ
is written by highly dedicated (and, some would say, eccentric)
people.  Many users find all those braces become very tedious very
quickly, and would really rather not type them all.
<p/>An alternative structure, that doesn&#8217;t violate the design of LaTeX,
is to say <code>\</code><code>fred</code><code>\</code><code> </code> &#8212; the <code>\</code><code> </code> command is &#8220;self
terminating&#8221; (like <code>\</code><code>\</code>) and you don&#8217;t need braces after
<em>it</em>.  Thus one can reduce to one the extra characters one needs
to type.
<p/>If even that one character is too many, the package <i>xspace</i>
defines a command <code>\</code><code>xspace</code> that guesses whether there should have
been a space after it, and if so introduces that space.  So

&#8220;<code>fred<code>\</code><code>xspace</code> jim</code>&#8221; produces &#8220;fred jim&#8221;, while
&#8220;<code>fred<code>\</code><code>xspace</code>. jim</code>&#8221; produces &#8220;fred. jim&#8221;.  Which
usage would of course be completely pointless; but you can incorporate
<code>\</code><code>xspace</code> in your own macros:
<blockquote>
<pre>
\usepackage{xspace}
...
\newcommand{\restenergy}{\ensuremath{mc^2}\xspace}
...
and we find \restenergy available to us...
</pre>

</blockquote><p>
The <code>\</code><code>xspace</code> command must be the last thing in your macro
definition (as in the example); it&#8217;s not completely foolproof, but it
copes with most obvious situations in running text.
<p/>The <i>xspace</i> package doesn&#8217;t save you anything if you only use
a modified macro once or twice within your document.  In any case, be
careful with usage of <code>\</code><code>xspace</code> &#8212; 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&#8217;t).  Of course, no command built into LaTeX or into any
&#8220;standard&#8221; class or package will use <code>\</code><code>xspace</code>.
<dl>
<dt><tt><i>xspace.sty</i></tt><dd>Distributed as part of <a href="http://www.tex.ac.uk/tex-archive/macros/latex/required/tools.zip">macros/latex/required/tools</a> (or <a href="http://www.tex.ac.uk/tex-archive/macros/latex/required/tools/">browse the directory</a>); <a href="http://www.tex.ac.uk/tex-archive/help/Catalogue/entries/xspace.html">catalogue entry</a>
</dl>
<p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=xspace">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=xspace</a>
</body>