问题描述
在 tabularx 环境中用 Lua 变量计算行数总是给我应该的倍数:
\documentclass{report}
\usepackage{environ}
\usepackage{tabularx}
\NewEnviron{funds}{%
\directlua{rows = 0}
\begin{tabularx}{2cm}{ |X|c| }
\BODY
\end{tabularx}
}
\newcommand\fundsadd{%
\directlua{rows = rows + 1}
a & b \tabularnewline
}
\begin{document}
\begin{funds}
\fundsadd
\fundsadd
\fundsadd
\end{funds}
Number of rows: \directlua{tex.sprint{rows}}
\end{document}
结果是
但行数应该是 3。它在表格环境中工作正常。有没有我遗漏的关于 tabularx 的东西?
解决方法
tabularx
需要多次处理内容。您可以通过在 tabularx 开头而不是之前重置计数器来解决此问题。
% !TeX TS-program = lualatex
\documentclass{report}
\usepackage{environ}
\usepackage{tabularx}
\NewEnviron{funds}{%
\begin{tabularx}{2cm}{ |X|c| }
\directlua{rows = 0}
\BODY
\end{tabularx}
}
\newcommand\fundsadd{%
\directlua{rows = rows + 1}
a & b \tabularnewline
}
\begin{document}
\begin{funds}
\fundsadd
\fundsadd
\fundsadd
\end{funds}
Number of rows: \directlua{tex.sprint{rows}}
\end{document}