# How can I avoid extra whitespace after using a macro before punctuation?

I have a document in which certain frequently-occurring entity names are replaced with macros, e.g.:

\newcommand{\entity}{\emph{Name of Entity}}
...
Lorem ipsum dolor sit \entity, consequat consectetur.


If I define the command like this I get correct formatting if \entity is immediately followed by punctuation (as above) but incorrect formatting otherwise (e.g. Lorem \entity ipsum becomes "Lorem Name of Entityipsum"). If I include the space in the definition of \entity (note space between closing brackets):

\newcommand{\entity}{\emph{Name of Entity} }


I get the opposite behaviour, proper spacing only when not followed by punctuation.

I assume there's some magick command I'm not including in the macro to achieve something sensible?

-
Have a look at tex.stackexchange.com/questions/25820/… – lockstep Mar 22 '12 at 16:20
lockstep's link is good; you want to look at the answer that suggests \xspace. – barbara beeton Mar 22 '12 at 16:22
Also have a look at the accepted answer. ;-) – lockstep Mar 22 '12 at 16:23
@lockstep -- \xspace is exactly what I'm after, thanks. Would you care to add that as an answer so I can mark it accepted? – Doches Mar 22 '12 at 16:32
@Doches It seems nobody but me likes the "delimited macro" approach. ;-) – lockstep Mar 22 '12 at 16:33

Although I personally prefer the "delimited macro" approach described here, you will probably want to use the \xspace macro provided by the package of the same name.

-

Put a backslash or an empty group after \entity when you want a space.

\newcommand{\entity}{\emph{Name of Entity}}
...
Lorem \entity\ ipsum
Lorem \entity{} ipsum

-
That works, but I'd greatly prefer a way to make the macro behave sensibly that doesn't require me to manually encode spaces. – Doches Mar 22 '12 at 16:20
You could put the "\ " in the command declaration of course if you know that you always want the space there... – jonalv Mar 22 '12 at 16:25
That's the problem I'm trying to solve, @jonalv -- I would like to use the same macro with and without a following space, and would like LaTeX to figure out which is which. If I define \entity as \newcommand{\entity}{\emph{Name of Entity} } I also get this behaviour ("\ " seems equivalent in command definitions to " "). – Doches Mar 22 '12 at 16:28