summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-xspace.html
blob: ff4cbfb70d1adfcb8560c01875446a9fe5c10243 (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&rsquo;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&rsquo;t violate the design of LaTeX,
is to say <code>\</code><code>fred</code><code>\</code><code> </code> &mdash; the <code>\</code><code> </code> command is &ldquo;self
terminating&rdquo; (like <code>\</code><code>\</code>) and you don&rsquo;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

&ldquo;<code>fred<code>\</code><code>xspace</code> jim</code>&rdquo; produces &ldquo;fred jim&rdquo;, while
&ldquo;<code>fred<code>\</code><code>xspace</code>. jim</code>&rdquo; produces &ldquo;fred. jim&rdquo;.  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&rsquo;s not completely foolproof, but it
copes with most obvious situations in running text.
<p/>The <i>xspace</i> package doesn&rsquo;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> &mdash; 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&rsquo;t).  Of course, no command built into LaTeX or into any
&ldquo;standard&rdquo; 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="ftp://cam.ctan.org/tex-archive/macros/latex/required/tools.zip">macros/latex/required/tools</a> (<a href="ftp://cam.ctan.org/tex-archive/macros/latex/required/tools.tar.gz">gzipped tar</a>, <a href="http://www.tex.ac.uk/tex-archive/macros/latex/required/tools/">browse</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>