脚注中不尊重 \setbool 吗?

脚注中不尊重 \setbool 吗?

有人可以解释为什么在命令中使用时不尊重\setbool(from )etoolbox仅有的用于脚注?

\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}

答案1

正确的定义\longshort应该是:

\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}%
}

其中ls设置为true带有\global前缀。否则,赋值不会在其所调用的组中继续存在。此外,ls定义为外部 \longshort

相关内容