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 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?

share|improve this question
2  
Have a look at tex.stackexchange.com/questions/25820/… –  lockstep Mar 22 '12 at 16:20
1  
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
show 1 more comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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
share|improve this answer
    
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
add comment

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.