I have a feeling this is another of my questions that is going to be a "Duh!!" kind of answer, but I can't seem to figure out how to do this properly: I want to put a wrapper around a macro that uses \xspace, but behaves differently upon the first use of the macro. The following does not quite produce the correct result upon first use, and leaves an extra space:

enter image description here

I'd prefer to have all the formatting controlled by one macro: \NameStyle, but am open to options.






The first name is \Name{Peter}.

The name \Name{Peter},  also known as some other name.

The name \Name{Peter} Grill is well known in TeX circles.
  • 3
    The problem is a spurious space after \global\togglefalse{FirstUseOfNameStyle}. But what should \xspace do? It's completely useless, of course. – egreg May 20 '12 at 2:35
  • @GonzaloMedina It was a very particular question and a particular situation: the immediate answer was easy and I was awake in the middle of the night because of an earthquake. – egreg May 20 '12 at 9:17

First of all there's a spurious space in your code, the one on the



Second: \xspace does nothing there, for a space after \Name{Peter} won't be gobbled anyway. The macro \xspace is only (dubiously) useful for macros without arguments.

If your aim is to define a macro that acts similarly to the "acronym resolver macros", then you can do it quite easily:

  \ifcsname peter@\detokenize{#1}\endcsname
    \global\expandafter\let\csname peter@\detokenize{#1}\endcsname\@empty

(Don't use this in "fragile contexts" such as section titles and captions that would trigger the "first appearance" in unwanted places; it can be corrected, if needed.)

So the first appearance of \Name{Peter} will print the name in red, as it will the first appearance of \Name{egreg}.

You can alias a call of \Name{Peter} by saying


and here \xspace would do its job.


egreg has pointed out the problem and the needless \xspace in your solution. But I still find your solution inefficient. The following solution is copied from ltxtools2 package.


\newname\peter{Peter Gill}[Peter]
\newname\pics{Production and Inventory Control Society}[PICS]

\peter said it. \peter did it. It was brought here by \peter. 
It was taken there by \peterlong, and not \petershort.

What connects \TeX\ with \pics? \pics deals with turning raw materials into 
finished goods that \peter buys from the shops.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.