我按照描述在 ConTeXt 中使用 lua这里我有这段代码,它只是打印$a x + b = 5$
wherea
并b
替换
\startitemize[a, columns]
\startluacode
context.startitem()
context.startimath()
context.getvariable("mynamespace", "a")
context("x + ")
context.getvariable("mynamespace", "b")
context("= 5")
context.stopimath()
context.stopitem()
\stopluacode
\stopitemize
我尝试简化:
这不起作用,因为
x
没有打印:context("%s x", context.getvariable("mynamespace", "a"))
那也不是(似乎我无法指定这一点):
local a = context.getvariable("mynamespace", "a") context("%s x ", a)
有人能帮忙简化表达吗?
答案1
如果你要在 Lua 端制作练习,你甚至不需要 TeX 宏来存储内容。这可能是一个起点:
\starttext
\startluacode
--Let's use a global namespace
--Variables will be accessible in any \startluacode ... \stopluacode block
userdata = userdata or {}
userdata.vars =
{
["a"] = 7,
["b"] = -3,
["c"] = -4,
["d"] = 3 -- Add more if you need it
}
--Locals avoid clashes and allow shortcuts
--They are defined only inside \startluacode ... \stopluacode blocks
local vars = userdata.vars
--%i is an integer and %I is a signed integer (it always prints the sign)
local fmt = string.formatters["%i x %I = 5"] --see cld-mkiv for reference
local ctx = context
ctx.startitem()
ctx.m(fmt(vars["a"], vars["b"]))
ctx.stopitem()
ctx.startitem()
ctx.m(fmt(vars["c"], vars["d"]))
ctx.stopitem()
\stopluacode
\stoptext
答案2
由此官方的文档,它的工作原理:
context.startitem()
-- Recuperam els paràmetres
local a = tokens.getters.macro(tokens.getters.macro("??variables") .. "mynamespace"..":".."a")
local b = tokens.getters.macro(tokens.getters.macro("??variables") .. "mynamespace"..":".."b")
local c = tokens.getters.macro(tokens.getters.macro("??variables") .. "mynamespace"..":".."c")
context("$ %s x + %s = %s $", a, b, c)
context.stopitem()