将 Tex 字符串转换为 Tex 编号

Converting Tex string in Tex number

我在需要数值的参数中使用自动生成的 Tex 字符串时遇到问题(例如,在 ifthenelse 比较中)。这是一个示例最小代码:

\newcommand\testC{123}
\ifthenelse{\testC<0}{negative}{positive} % works fine !

\newcommand{\testD}{\luaexec{tex.write("123")}} % write to avoid the print carriage return - produces also 123 as \testC
\testD % prompt 132 just as \testC "apparently"
\ifthenelse{\testD<0}{negative}{positive} % error "! Missing number, treated as zero"

\newcounter{compteur}
\setcounter{compteur}{\testD} % error "! Missing number, treated as zero"
\ifthenelse{\thecompteur<0}{negative}{positive}

我找不到将字符串转换为接受算术比较(和其他操作)的数字的方法。

注意 \luaexec(需要 \usepackage{luacode})是不可扩展的,所以它不能用于 (Lua)TeX 期望扩展后的 <number> 的地方。

\documentclass{article}
\usepackage{luacode}
\usepackage{ifthen}

\begin{document}

\newcommand\testC{123}
\ifthenelse{\testC<0}{negative}{positive} % works fine !

\newcommand{\testD}{\directlua{tex.sprint("123")}} % write to avoid the print carriage return - p$

\testD % prompt 132 just as \testC "apparently"

\ifthenelse{\testD<0}{negative}{positive} % error "! Missing number, treated as zero"

\newcounter{compteur}
\setcounter{compteur}{\testD} % error "! Missing number, treated as zero"
\ifthenelse{\value{compteur}<0}{negative}{positive}

\end{document}

最好在测试中使用 \value{compteur}