我有一些 Lua 代码,它在某些文本中搜索“①”和“②”并仅打印显示在之间的文本:
\startluacode
userdata = userdata or {}
function userdata.printbetween(str, m1, m2)
m1 = m1 or "①"
m2 = m2 or "②"
mat_pat = string.format('%s(.*)%s', m1, m2)
str = string.match(str, mat_pat) or str
context(str)
end
\stopluacode
\define\sentence{This is ① a final ② test sentence.}
\define[1]\textbetween{%
\ctxlua{userdata.printbetween([===[#1]===])}%
}
\starttext
\startitemize[n]
\item \textbetween{This is ① a test ② sentence.}
\item \textbetween{This is ① another test ② sentence.}
\item \textbetween{\sentence}
\stopitemize
\stoptext
这应该打印:
1. a test
2. another test
3. a final
相反,这是打印:
1. a test
2. another test
3. This is ① a final ② test sentence.
当我给Lua一个变量(例如)时\sentence
,它似乎处理“\sentence”而不是其中包含的数据\sentence
。
我怎样才能让 Lua 检查变量里面的内容而不是仅仅检查变量的名称?
答案1
\define
使得定义无法扩展。相反,使用
\def\sentence{....}