将星号传递给 lua

将星号传递给 lua

我希望这是一个简单的问题,但我找不到相关文档。也许我搜索的关键字不对。

我想将星号*选项传递给我的 lua 代码。根据旧项目的代码,我认为它是一个布尔值。\bool_if:NTF #1 {do true stuff}{do false stuff}

但是将星号作为参数传递会破坏代码。

\NewDocumentCommand{\addline}{smm}{\directlua{myluacodefile.addline(#1,"#2","#3")}}%

这是一个最小的“工作”示例

主文本

\documentclass[letterpaper]{article}
\usepackage{xparse}%for advanced command declaration

\directlua{myluacodefile = require("myluacodefile")}%this links the .lua
\newcommand{\sanitycheck}[2]{\directlua{myluacodefile.sanitycheck(#1,#2)}}%

%\NewDocumentCommand{\addline}{mm}{\directlua{myluacodefile.addline("A","#1","#2")}}%turn this on to see the code "work"
\NewDocumentCommand{\addline}{smm}{\directlua{myluacodefile.addline(#1,"#2","#3")}}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

hello

\sanitycheck{1}{2}

\addline{first}{second}

\addline*{third}{fourth}

\end{document}

mylua代码文件.lua

local myluacodefile = {}--lua module start

name = "Sanity Check"

function myluacodefile.sanitycheck(a,b)
    c=a+b
    tex.print(c)
    tex.print(name)
end


function myluacodefile.addline(star, alpha, beta)
    local testa = alpha
    local testb = beta
    tex.print(testa)
    tex.print(testb)
end

return myluacodefile--lua module end

答案1

如果我理解你的问题正确的话,这可能有效:

主文本

xparse允许定义带星号和不带星号的命令。正如您所说,星号表示布尔值,因此您必须使用 分别定义带星号和不带星号形式的命令\IfBooleanTF#1{true}{false}。至于 Lua 方面,您必须包含第三个参数。

%!TEX program = lualatex
\documentclass[letterpaper]{article}
\usepackage{xparse}%for advanced command declaration

\directlua{myluacodefile = require("myluacodefile")}%this links the .lua
%\NewDocumentCommand{\addline}{mm}{\directlua{myluacodefile.addline("A","#1","#2")}}%turn this on to see the code "work"
\NewDocumentCommand{\addline}{smm}{%
%If you need some code to be executed independently of the star, put it here.
\IfBooleanTF#1{%
\directlua{myluacodefile.addline("star","#2","#3")}
}{\directlua{myluacodefile.addline("nostar","#2","#3")}}
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

hello

\addline{first}{second}

\addline*{third}{fourth}

\end{document}

mylua代码文件.lua

在 Lua 方面,我们添加了一个参数来读取星号或它的缺失。

local myluacodefile = {}--lua module start

name = "Sanity Check"

function myluacodefile.sanitycheck(a,b)
    c=a+b
    tex.print(c)
    tex.print(name)
end


function myluacodefile.addline(star, alpha, beta)
    local warning = "There's no star!"
    local check = "There's a star!"
    local testa = alpha
    local testb = beta
--I put this as an example, but you can define your own macros according to your needs
    if star == "nostar" then 
    --Unstarred
     tex.print(warning) 
    elseif star == "star" then
    --Starred 
     tex.print(check) 
    end
    tex.print(testa)
    tex.print(testb)
end

return myluacodefile--lua module end

在此处输入图片描述

答案2

xparse在过去的实现中,参数的代码可能s通过条件传递。但永远不应该依赖实现的细节。

使用s-argument 的“官方”方法是通过

\IfBooleanTF{#1}{<code for star>}{<code for nostar>}

或通过\bool_if:nTF如果传递到级别expl3。绝对不是\bool_if:NTF

在您的代码中,Lua 函数被传递 或\BooleanTrue\BooleanFalse这对 Lua 来说毫无意义。实际上,您可以轻松发现\BooleanTrue和分别\BooleanFalse\chardef值为 1 和 0 的标记,但这些是应该注意的实现细节绝不值得信赖。

你可以做

\NewDocumentCommand{\addline}{smm}{%
  \directlua{myluacodefile.addline("\IfBooleanTF{#1}{star}{nostar}","#2","#3")}%
}

因为该结构\IfBooleanTF是完全可扩展的。

相关内容