问题描述
我用lua做一些复杂的工作来为Tex/LaTex中的宏准备参数.
I use lua to make some complex job to prepare arguments for macros in Tex/LaTex.
第一部分这是一个愚蠢的最小示例:
Part IHere is a stupid minimal example :
\newcommand{\test}{\luaexec{tex.print("11,12")}}% aim to create 11,12
\def\compare#1,#2.{\ifthenelse{#1<#2}{less}{more}}
\string\compare11,12. : \compare11,12.\\ %answer is less
\string\test : \test\\ % answer is 11,12
\string\compare : \compare\test. % generate an error
最后一行创建一个错误.显然,Tex没有检测到\ test中包含的,".
The last line creates an error. Obviously, Tex did not detect the "," included in \test.
如何将\ test理解为11,后跟,再跟12,而不是字符串11,12,最后用作\ compare的正确格式参数?
How can I do so that \test is understood as 11 followed by , followed by 12 and not the string 11,12 and finally used as a correctly formed argument for \compare ?
推荐答案
TeX的工作方式存在一些误解.
There are several misunderstandings of how TeX works.
您的\compare
宏要查找的内容后面是逗号,然后是句点.但是,当您致电
Your \compare
macro wants to find something followed by a comma, then something followed by a period. However when you call
\compare\test
没有找到
逗号,因此TeX一直在寻找它,直到找到文件的结尾或\par
(或者也有一个空行).请注意,TeX 从不在查找宏参数时会扩展宏.
no comma is found, so TeX keeps looking for it until finding either the end of file or a \par
(or a blank line as well). Note that TeX never expands macros when looking for the arguments to a macro.
你可能会做
\expandafter\compare\test.
假设\test
立即以所需的格式扩展为令牌,但不会扩展,因为\test
的扩展为
provided that \test
immediately expands to tokens in the required format, which however don't, because the expansion of \test
is
\luaexec{tex.print("11,12")}
,并且用大括号隐藏了逗号,因此不算在内.但这仍然无济于事.
and the comma is hidden by the braces, so it doesn't count. But it wouldn't help nonetheless.
问题是一样的:当您这样做
The problem is the same: when you do
\newcommand{\test}{\luaexec{tex.print("11,12")}}
该参数未扩展 .您可能在\edef
中使用了扩展定义",但是问题是\luaexec
不能完全扩展.
the argument is not expanded. You might use "expanded definition" with \edef
, but the problem is that \luaexec
is not fully expandable.
如果愿意
\edef\test{\directlua{tex.sprint("11,12")}}
然后
\expandafter\compare\test.
会工作的.
这篇关于在lua中构建Latex/Tex参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!