每次调用宏时,我都想在两个值之间切换。我可以让它工作,除非它在表格中。我想让它在表格中工作。我认为这与范围有关。
\documentclass{article}
\newif\ifToggle\Togglefalse
\newcommand\checktoggle{
\ifToggle \Togglefalse
Toggle was true%
\else \Toggletrue
Toggle was false%
\fi}
\begin{document}
This seems to work:\par
\checktoggle.\par
\checktoggle.\par
\checktoggle.\par
\bigskip
But not in tabular:
\begin{tabular}{ll}
\checktoggle & 1 \\
\checktoggle & 2
\end{tabular}
\end{document}
答案1
你可以把它放在\global
每个用途的前面,依靠这个事实,即所有的宏都是通过纯扩展来工作的,所以它\global
最终适用于正确的分配,但更简单、更安全的是将切换定义为全局开关(例如,\if@nobreak
参见 latex 格式)
\documentclass{article}
%\newif\ifToggle
\def\Toggletrue{\global\let\ifToggle\iftrue}
\def\Togglefalse{\global\let\ifToggle\iffalse}
\Togglefalse
\newcommand\checktoggle{%
\ifToggle \Togglefalse
Toggle was true%
\else \Toggletrue
Toggle was false%
\fi}
\begin{document}
This seems to work:\par
\checktoggle.\par
\checktoggle.\par
\checktoggle.\par
\bigskip
But not in tabular:
\begin{tabular}{ll}
\checktoggle & 1 \\
\checktoggle & 2
\end{tabular}
\end{document}