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?

5
  • 2
    Have a look at tex.stackexchange.com/questions/25820/…
    – lockstep
    Mar 22, 2012 at 16:20
  • 1
    lockstep's link is good; you want to look at the answer that suggests \xspace. Mar 22, 2012 at 16:22
  • @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, 2012 at 16:32
  • @Doches It seems nobody but me likes the "delimited macro" approach. ;-)
    – lockstep
    Mar 22, 2012 at 16:33
  • @lockstep -- \xspace doesn't work with plain tex. we oldtimers really do prefer a trailing backslash or a grouped {\command}, but we're a dwindling minority. Mar 22, 2012 at 16:39

2 Answers 2

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.

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
3
  • 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, 2012 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, 2012 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, 2012 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .