Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

MWE

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.)

share|improve this question
add comment

1 Answer

up vote 14 down vote accepted

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.

enter image description here

share|improve this answer
    
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
1  
Another trick which is common with TeX is something like \newcommand{\SL}{\textit{SL}~first\renewcommand\SL{\textit{SL}~second}}. ...although a global version is easier with plain TeX than with basic LaTeX: \gdef\SL{\textit{SL}~first\gdef\SL{\textit{SL}~second}}. –  Stephan Lehmke Jun 11 '12 at 20: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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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