7

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?

lockstep
  • 250,273
Doches
  • 460

2 Answers2

6

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.

lockstep
  • 250,273
4

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
Ian Thompson
  • 43,767
  • 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