views:

3513

answers:

4

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?

+17  A: 

Yes, look at the xspace package.

\usepackage{xspace}

And later on...

\newcommand{\arnold}{Arnold Schwarzenegger\xspace}

Uri
Thanks, this is what I needed.
Kaarel
It's a tricky one. My advisor years ago told me about it or I would never have found it.
Uri
A: 

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?

Baltimark
This won't work if \arnold is followed by a punctuation mark.
Zach Scrivena
It won't work how? If I write \arnold!, I get "Arnold Schwarzenegger !" the space is still there.
Baltimark
I don't want a space if a punctuation mark follows. "xspace" handles that.
Kaarel
+15  A: 

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.

bluebrother
"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
... 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
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
A: 

just what I'm looking for.. thanks a lot..

Gimi