Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm certain I have read that xspace can cause more problems than it solves. But I cannot find anything to that effect either on this site or the web. Are there any drawbacks or risks associated with it?

share|improve this question
    
Possibly outdated: groups.google.com/forum/?fromgroups=#!topic/comp.text.tex/… –  lockstep Dec 11 '12 at 17:45
7  
The example @lockstep refers to is \newcommand{\test}{\emph{test}\xspace} and still the problem exists: the italic correction is inserted in situations such as \test,, which is wrong. I never recommend (nor use myself) xspace. :) –  egreg Dec 11 '12 at 17:59
1  
@egreg: so you always insert spaces manually, or you do something clever? –  Mohan Dec 11 '12 at 19:09
1  
@Mohan I write \test{} if a space follows. That's all. –  egreg Dec 11 '12 at 20:37
3  
\newcommand{\newentity}[3][/]{\@ifdefinable{#2}{\def#2#1{#3}}} and then \newentity{\foo}{\emph{foo}} will define \foo to require / after it; or \newentity[|]{\bar}{\textbf{bar}} would require \bar|. As usual \makeatletter and \makeatother are necessary around the definition of \newentity; \newentity will throw an error if the command is already defined. –  egreg Dec 11 '12 at 22:54

2 Answers 2

up vote 64 down vote accepted
+250

I originally implemented xspace a long time ago (for LaTeX 2.09) as, like most LaTeX beginners, I'd had a document where I had omitted a {} or \ and so had a missing space in the final document.

The original version used a very simple nested test for following punctuation, but it could get the test wrong in some (well rather a lot really) of cases. Morten made the tests a lot better so the current version makes the correct choice in more cases and is more easily customised) however making the tests more complicated highlights the problem with this kind of package.

The rule in TeX is really quite simple, after a command name that uses letters (as opposed to single character command names using non-letters such as \$) white space is ignored. It is easy to forget to use \ or {} but the result of forgetting is very predictable, you lose white space in the result. Conversely with xspace the macro will get the correct space most of the time, but it isn't easy to predict when it will get it wrong, and so it's much harder to learn to enter the markup in a way that is always correct rather than having to always visually check for missing space, which rather negates the purpose of the command.

So, if you find it useful, fine, it's there. But personally I wouldn't recommend it.

share|improve this answer
35  
Please add this discussion to the package documentation. –  Martin Schröder Dec 18 '12 at 19:16

It has been mentioned on the site before, however, it seems to me that it can have its place here. You can define your commands in such a way that you can't forget the space at all. This is done by defining:

\def\ES/{Einstein}

(you can't use \newcommand for this). This way, the proper syntax in-document is

The physisist \ES/ proved the so-called \ES/'s theorem.

You can test yourself that the space in \ES/ proved does not get eaten, because in real, the trailing / is not part of the name of the macro, but rather a sort-of "obligatory fixed-valued argument", that is called macro delimiter in TeX's jargon. If you omit this / and try to write \ES proved, you get a not-so-cryptic error message

Use of \ES doesn't match its definition

which means exactly what it says: You defined \ES it to be followed by / and this is missing.


However, the best thing to do is not to define shorthands like \ES for Einstein. Then you don't need xspace and you can't reach any such problems.

share|improve this answer
3  
The Shorthand for Einstein is maybe superfluous but there are many cases where a shortcut comes in handy. E.g. if you want to format the name of something always in sans you should define a shortcut for \textsf{somename}. If your publisher then thinks that sans is bad simply change the definition instead of search for all occurrences. Furthermore defining such shortcuts ensures a consistent layout and fits (in my eyes) perfectly in the principles of logical markup … –  Tobi Nov 26 '13 at 14:29
    
@Tobi Then, I would go with \N{Einstein} or a similar short-named command. –  tohecz Nov 26 '13 at 18:09
    
Sure if there are several names to be mentioned. But if you have only one name, like the name of a package, it should be a command. For example I use \newcommand{\metrix}{\textcolor{red}{\textbf{\textsf{m\u{e}trix}}}\xspace} in my metrix package, maybe even \LaTeX itself can be an example for that ;-) –  Tobi Nov 26 '13 at 18:45
    
@Tobi And my definition in that case would be without xspace. But I agree, there are cases when you use a special macro, but IMHO they are exceptional. –  tohecz Nov 26 '13 at 18:52
1  
“… they are exceptional” depends on how you work and what you work on – I guess ;-) … I wasn’t aware of the xspace discussion yet, but I consider removing the \xspace :-) –  Tobi Nov 26 '13 at 19:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.