布尔值在表格环境中失败

布尔值在表格环境中失败

我已经找到了解决这个问题的办法,但也许有人知道一种更简洁的解决方法。我使用的是 TexLive 2009 中的标准包。

这是不起作用的代码:

\documentclass{article}
\usepackage{ifthen}
\newboolean{test}
\setboolean{test}{false}
\newcommand{\testval}{\ifthenelse{\boolean{test}}{yes}{no}}
\begin{document}
  \begin{tabular}{ll}
    Should be no & \testval \\
    Change value & \setboolean{test}{true} \\
    Shoule be yes & \testval \\
  \end{tabular}
\end{document}

我在 ifthen 包描述中没有看到任何关于此行为的提及。我还注意到,例如,您不能使用\newcommandand\isundefined来解决这个问题。

到目前为止,我的解决方法是使用计数器,然后测试\value{counter}=0。唯一的困难是没有\providecounter使用布尔值或命令的方法,所以我写了 providecounter.sty:

\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{providecounter}
          [2010/11/16 my hack to implement a providecounter command]

\def\providecounter#1{\@ifundefined{c@#1}{\newcounter{#1}}\relax}

所以,有两个问题:a. 为什么在表格环境中可以工作\setboolean\newcommand不工作?b. 计数器是否或多或少不在乎它们在哪里定义?

答案1

似乎重置布尔值仅在本地有效(即在同一个表格单元格内)。使用前缀\global会产生错误消息。虽然我没有完整的解释,但我有一个(解决方法?)解决方案:使用电子工具箱包及其命令而不是 ifthen - 然后才使用前缀\global

\documentclass{article}
\usepackage{etoolbox}
\newbool{test}
\setbool{test}{false}
\newcommand{\testval}{\ifbool{test}{yes}{no}}
\begin{document}
  \begin{tabular}{ll}
    Should be no & \testval \\
    Change value & \global\setbool{test}{true} \\
    Should be yes & \testval \\
  \end{tabular}
\end{document}

编辑:我思考我明白。重置布尔值显然遵循正常的 LaTeX 范围规则 - 即,如果在组内进行重置,其效果将仅限于该组。另一方面,“更改计数器值的命令是全局声明”(Leslie Lamport - LaTeX。文档准备系统,第 98 页) - 这就是您使用计数器的解决方法成功的原因。

相关内容