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.

Can someone explain why \setbool (from etoolbox) is not respected when used in a command only used in a footnote?

\documentclass[12pt]{article}

\usepackage{etoolbox}
\newcommand{\longshort}[1][]{%
  \providebool{ls}%
  \ifbool{ls}%
  % bool = true = not first time
  {This is the #1 time\ldots}%
  % bool = false = first time
  {This is the first time the command is used!}%
  \setbool{ls}{true}%
}

\begin{document}

1:% \longshort%
\footnote{\longshort[2nd]}

2:% \longshort[3rd]
\footnote{%
% \providebool{ls}%   uncomment these two lines to get
% \setbool{ls}{true}% expected behaviour
\longshort[4th]}

\end{document}
share|improve this question
    
Is the intent to follow a different conditional branch at the first call compared to subsequent calls? –  Werner Mar 28 '13 at 5:04
    
@Werner -- Yes. I thought it'd be an easy way to 'track' whether a custom citation command had been used or not; if not, then give a 'full' citation; if yes, then give an abbreviated version. (This is why it's only used in footnotes.) –  jon Mar 28 '13 at 5:06

1 Answer 1

up vote 4 down vote accepted

The correct definition of \longshort should be:

\providebool{ls}%
\newcommand{\longshort}[1][]{%
  \ifbool{ls}%
  % bool = true = not first time
  {This is the #1 time\ldots}%
  % bool = false = first time
  {This is the first time the command is used!}%
  \global\booltrue{ls}%
}

where ls is set to true with a \global prefix. Otherwise the assignment doesn't survive the group it is called within. Also, ls is defined outside \longshort.

share|improve this answer
    
Hmm, yes. And \providebool has to be outside the command definition. I'd rather if everything could be inside the \newcommand, but it's not that big a deal I guess. –  jon Mar 28 '13 at 5:11
1  
@jon You'd make LaTeX spin its wheels for nothing at every call of \longshort trying to see whether the ls boolean is already defined. The macro \longshort uses the boolean, which therefore should be defined before it. And \newbool is better: you'll be warned if some package defines it; otherwise you won't get any warning/error and you may end up disrupting the package's and your own work. –  egreg Mar 28 '13 at 11:08
    
@egreg -- That makes sense. I actually had a working solution before (not quite as compressed/elegant), but I got obsessed with making everything self-contained. The thing that was bugging me was simply that there seemed to be no way for a boolean defined (provide-d) in a footnote to be seen globally. But I wasn't thinking at all about making LaTeX do extra work all the time for such a small gain.... –  jon Mar 28 '13 at 15:32

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.