2015-05-05 46 views
2

我有問題使用自動生成的Tex字符串在需要數字值的參數中(例如在ifthenelse比較中)。下面是一個示例最少的代碼:轉換TEX字符串中的Tex字符串

\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} 

我不能找到一種方法,從字符串轉換爲接受的算術比較(等操作)的數字。

回答

2

請注意,\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}更好。