I am trying to use a global boolean variable b
within a LaTeX3 function. I would like my function to print F
if b
is false and toggle it to true, and to print T
if b
is true.
This works great in text and regular math, but always prints T
if I try to use it within amsmath
environments like gather
or align
. It looks like amsmath
is expanding/evaluating the contents of its environments at least once before the final render. How can I avoid this / fix my function?
Here is a MWE, where I stripped out good practices like \NewDocumentCommand
and \cs_new_protected:Npn
to get it as minimal as possible:
\documentclass{minimal}
\usepackage{amsmath}
\usepackage{etoolbox}
\ExplSyntaxOn
\NewDocumentCommand{\defit}{m}{
\bool_new:c { g_it_#1_used }
}
\NewDocumentCommand{\getit}{m}{
\bool_if:cTF { g_it_#1_used } { T } { F\bool_gset_true:c { g_it_#1_used } }
}
\ExplSyntaxOff
\begin{document}
\defit{test}\getit{test}\getit{test}\getit{test}
\defit{eqn}
\[
\getit{eqn} \getit{eqn} \getit{eqn}
\]
\defit{ams}
\begin{gather*}
\getit{ams} \getit{ams} \getit{ams}
\end{gather*}
\end{document}
The expected output is FTT
three times. Instead, I get FTT
, FTT
, TTT
.