Latex 在TABLARX(但不是TABLARX)中使用Lua进行计数是应该的三倍

Latex 在TABLARX(但不是TABLARX)中使用Lua进行计数是应该的三倍,latex,Latex,在TABLARX环境中,使用Lua变量计算行数时,我总是得到它应该是的倍数: \documentclass{report} \usepackage{environ} \usepackage{tabularx} \NewEnviron{funds}{% \directlua{rows = 0} \begin{tabularx}{2cm}{ |X|c| } \BODY \end{tabularx} } \newcommand\fundsadd{%

在TABLARX环境中,使用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。它在表格环境中运行良好。关于tablerx,我有什么遗漏吗?

tablerx
需要多次处理内容。您可以通过在表格开始时而不是之前重置计数器来解决此问题

% !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}

Tablerx需要对内容进行多次处理,以测量所需的空间并确定列的最佳宽度。tex计数器将开箱即用:@samcarter_is_at_topanswers.xyz我担心会发生这种情况。有趣的是,如果使用标准计数器,则不会发生这种情况。@samcarter_is_at_topanswers.xyz是的,没错,计数器工作。但在我的实际问题中,我使用Lua变量来跟踪不同的费用,使用Lua变量使整个代码更加灵活。TeX是图灵完全的,灵活性只是您决定的函数:)