如果在环境中不切换

如果在环境中不切换

我有以下环境来创建发票。它运行完美,只是行不会在浅灰色和深灰色之间切换(它们保持浅色),所以我认为这\iflight是真的。如果我将初始\lighttrue命令更改为,\lightfalse它们只会保持深灰色。无论哪种方式,它们都不会交替。我在非环境中尝试了类似的命令,它成功了。是什么导致它无法切换?

\newcounter{total}
\newcounter{a}
\newcounter{b}
\newif\iflight
\newenvironment{invoice}[1]{%
  \setcounter{total}{0}
  \lighttrue
  \newcommand*{\additem}[3]{\noalign{\addtocounter{total}{{##2*100}+##3}}\iflight\rowcolor{black!5!white}\lightfalse\else\rowcolor{black!10!white}\lighttrue\fi ##1 && ##2.##3 \\}%
  \textbf{Invoice no.\ #1}

  \tabularx{\linewidth}{Xrr}
  \hline
  \rowcolor{black!20!white}\textbf{Desciption} && \textbf{Amount}\\
  \hline
}{%
  \hline
  \noalign{\setcounter{a}{\value{total}/100}}
  \noalign{\setcounter{b}{\value{total}-\value{a}*100}}
  \multicolumn{1}{r}{\textbf{TOTAL:}} & \cellcolor{blue!10!white} \textdollar & \cellcolor{blue!10!white} \arabic{a}.\ifnum\value{b}<10 0\fi\arabic{b} \\
  \endtabularx\par
}

答案1

由于表格cells被视为组,因此切换用该组保留本地TeX定义的条件,即将其粘贴在该单元格上,而下一个单元格(或行)则不会意识到这一变化。\newif\lighttrue

为了全局激活更改,必须应用\global\lighttrue或。\global\lightfalse

\documentclass{article}

\usepackage{calc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\newcounter{total}

\newcounter{a}
\newcounter{b}
\newif\iflight
\newenvironment{invoice}[1]{%
  \setcounter{total}{0}
  \lighttrue
  \newcommand*{\additem}[3]{\noalign{\addtocounter{total}{{##2*100}+##3}}\iflight\rowcolor{black!5!white}\global\lightfalse\else\rowcolor{black!10!white}\global\lighttrue\fi ##1 && ##2.##3 \\}%
  \textbf{Invoice no.\ #1}

  \tabularx{\linewidth}{Xrr}
  \hline
  \rowcolor{black!20!white}\textbf{Desciption} && \textbf{Amount}\\
  \hline
}{%
  \hline
  \noalign{\setcounter{a}{\value{total}/100}}
  \noalign{\setcounter{b}{\value{total}-\value{a}*100}}
  \multicolumn{1}{r}{\textbf{TOTAL:}} & \cellcolor{blue!10!white} \textdollar & \cellcolor{blue!10!white} \arabic{a}.\ifnum\value{b}<10 0\fi\arabic{b} \\
  \endtabularx\par
}


\begin{document}
\begin{invoice}{42}
  \additem{Data collection}{1500}{00}
  \additem{LaTeX book}{50000}{00}
  \additem{Answer on TeX.SX}{10000000}{00}
\end{invoice}
\end{document}

在此处输入图片描述

相关内容