Tell me more ×
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 have a definition

\newcommand{\arnold}{Arnold Schwarzenegger}

when I refer to it by

\arnold is a

it is rendered as

Arnold Schwarzeneggeris a

In order to have a space in front of "is" I would need to write

\arnold\ is a

Is there another and shorter way?

share|improve this question
Beware, the protected space \ isn't flexible (I think). – Limited Atonement Mar 10 '11 at 17:53

migrated from stackoverflow.com Oct 10 '11 at 10:16

4 Answers

up vote 71 down vote accepted

If you create a macro without arguments you should always invoke it with an empty statement after it: \arnold{}

The reason behind this is that LaTeX expects an argument directly after the macro (it's still in scanning mode for that macro). You need to break that using either a protected space (as you already wrote) or an empty statement {}. I'd recommend using an empty statement, as using a protected space can generate nasty effects -- for example, if that protected space is directly followed by a line break. In that case LaTeX might print two spaces instead which looks ugly and isn't wanted. Using an empty statement prevents this.

How you can add the space directly to the macro has already been answered, but do you really want this? You'd get into trouble as soon as the macro is to be followed by a punctuation mark (or if the macro is followed by a \footnote, etc.):

\arnold,
Arnold Schwarzenegger ,

I'd recommend going for the empty statement option -- one gets used to that quite fast.

share|improve this answer
2  
"xspace" is a tiny package which basically just contains a list of punctuation marks, making it not insert the space if there is a punctuation mark. – Kaarel Feb 4 '09 at 19:17
... but it also removes any space if it's in front of a punctuation mark, so if this is not wanted, then I can use the general {}-solution. – Kaarel Feb 4 '09 at 19:31
1  
xpace is fine, but this solution is simpler and requires less knowledge -- \comman{} is very standard notation. I prefer the simple, even though it means extra characters here and there. It better expresses what you're trying to say. – James A. Rosen Feb 7 '09 at 0:20
2  
Thanks for this. I have been creating two of each \newcommand, one with a space and one without, but I knew there had to be a way to make it smarter. Also, apparently you have to do this with some built in stuff, like \LaTeX{}, or you will get the same problem. – asmeurer Dec 9 '10 at 3:11
+1, thanks for that empty statement explanation. – hans0l0 Mar 19 at 15:17

Yes, look at the xspace package.

\usepackage{xspace}

And later on...

\newcommand{\arnold}{Arnold Schwarzenegger\xspace}
share|improve this answer
Thanks, this is what I needed. – Kaarel Feb 4 '09 at 19:05
It's a tricky one. My advisor years ago told me about it or I would never have found it. – Uri Feb 4 '09 at 19:16
1  
Note this will not work when you enclose it in braces again: \newcommand{\arnold}{{Arnold Schwarzenegger\xspace}} This would be useful in situations where you must enclose your commands anyway, e.g. in combination with the soul package: \hl{foo \arnold bar} would not work unless you enclose it in additional braces. – math Mar 29 '12 at 12:15
1  
Importantly related: Drawbacks of xspace – doncherry Dec 15 '12 at 19:28

Apart from using an empty statement

\hello{} world

You can also use a tilde as an explicit space:

\hello~world

Make sure not to put another space after the tilde, as this will result in two spaces.

share|improve this answer
4  
However, this would make the phrase hello world inseparable end of a line when otherwise LaTeX would put them in separate lines. This may not be the desired behavior. – Mobius Pizza Oct 10 '12 at 10:45

For this specific instance, can't you just write,

\newcommand{\arnold}{Arnold Schwarzenegger }

(there's a space after "egger".)

That works on my machine.

Or are you trying to do something more general that this doesn't work for, and this is just a toy example?

share|improve this answer
1  
This won't work if \arnold is followed by a punctuation mark. – Zach Scrivena Feb 4 '09 at 19:01
It won't work how? If I write \arnold!, I get "Arnold Schwarzenegger !" the space is still there. – Baltimark Feb 4 '09 at 19:04
5  
I don't want a space if a punctuation mark follows. "xspace" handles that. – Kaarel Feb 4 '09 at 19:07

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.