# First occurrence of a command

I'm writing some macros that need to behave differently after first occurrence. I decided to use the etoolbox package's toggle. My MWE is as follows:

\documentclass{article}
\usepackage{etoolbox}

\newtoggle{SL}

\newcommand{\SL}{%
\textit{SL}%
\nottoggle{SL}{~first}{~second}%
\toggletrue{SL}% switches to true after first occurrence!
}

\begin{document}
A footnote.\footnote{\SL}\par
\SL \par
\SL \par
\SL
\end{document}


As an output I get the following:

The problem, as you can see, is that when using the command in a footnote, the toggle does not occur. (Should the image not be clear enough, the footnote reads "1 SL first".)

What is going on in here? (I tried TeX's \newif with the same result, by the way.)

-

The solution is hidden in the package documentation:

This command [...] may be prefixed with \global.

The meaning of this is that usually \toggletrue will work locally, that is the setting is undone by the enclosing group. \footnote here defines a group. So you need to use

\global\toggletrue{SL}% switches to true after first occurrence!


in your macro to make sure the switch is always set globally.

-
Thanks, Stephan! It works fine. –  NVaughan Jun 10 '12 at 3:35
Just for curiosity---is there another way to accomplish this without toggles or booleans? –  NVaughan Jun 11 '12 at 19:00
Thanks! That's cool. I wasn't aware that one could redefine a command from within the very command! –  NVaughan Jun 11 '12 at 20:20