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:
I'd prefer to have all the formatting controlled by one macro: \NameStyle
, but am open to options.
Code:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{xspace}
\newtoggle{FirstUseOfNameStyle}%
\toggletrue{FirstUseOfNameStyle}%
\newcommand*{\NameStyle}[1]{\textit{#1}\xspace}%
\newcommand*{\NameStyleForFirstUse}[1]{\textcolor{red}{\NameStyle{#1}}}%
\newcommand*{\Name}[1]{%
\iftoggle{FirstUseOfNameStyle}{%
\NameStyleForFirstUse{#1}%
\global\togglefalse{FirstUseOfNameStyle}
}{%
\NameStyle{#1}%
}%
}%
\begin{document}
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.
\end{document}
\global\togglefalse{FirstUseOfNameStyle}
. But what should\xspace
do? It's completely useless, of course.