使用一个lua函数的输出作为tex文件中另一个函数的输入

使用一个lua函数的输出作为tex文件中另一个函数的输入

以下是代码。

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
function string(x)
tex.sprint(x)
end
function combine(x,y)
tex.sprint(x.." "..y)
end
\end{luacode*}
\newcommand{\test}[1]{\directlua{string(#1)}}
\newcommand{\othertest}[2]{\directlua{combine(#1,#2)}}
\test{"abc"}\\
\othertest{"abc"}{"def"}
\end{document}

这很好用。以下代码也很好用。

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
function string(x)
tex.sprint(x)
end
function combine(x,y)
tex.sprint(x.." "..y)
end
\end{luacode*}
\newcommand{\test}[1]{\directlua{string(#1)}}
\newcommand{\othertest}[2]{\directlua{combine(#1,#2)}}
\test{"abc"}\\
\othertest{"abc"}{"def"}\\
\def\str{"abc"}
\def\otherstr{"def"}
\othertest{\str}{\otherstr}
\end{document}

以下是另一个不起作用的代码。

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
function string(x)
tex.sprint(x)
end
function combine(x,y)
tex.sprint(x.." "..y)
end
\end{luacode*}
\newcommand{\test}[1]{\directlua{string(#1)}}
\newcommand{\othertest}[2]{\directlua{combine(#1,#2)}}
\test{"abc"}\\
\othertest{"abc"}{"def"}\\
\def\str\test{"abc"}
\def\otherstr\test{"def"}
\othertest{\str}{\otherstr}
\end{document}

我正在处理一些复杂的文档,我想将一个 lua 函数的输出用作 tex 文件本身中另一个函数的输入。我知道我可以在 lua 中做到这一点。但我正在处理一些通用的东西。假设我在 lualatex 中为两个数字定义了加法和减法函数。现在我想将一个函数的输出用于另一个函数。例如

 \def\x{2}
    \def\y{3}
    \add{x}{y}
\subtract{x}{y}

这会起作用。但是如果我想做

\subtract{\add{x}{y}}{y}

这就是我的意思。

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
function add(x,y)
return tex.print(x+y)
end
function subtract(x,y)
return tex.print(x-y)
end
\end{luacode*}
\newcommand{\add}[2]{\directlua{add(#1,#2)}}
\newcommand{\subtract}[2]{\directlua{subtract(#1,#2)}}
\add{3}{2}\\
\subtract{3}{2}
\subtract{{\add{3}{2}}{1}}
\end{document}

这行不通。我知道我可以在 lualatex 中定义另一个函数,它可以同时执行加法和减法。但正如我提到的,我想在 tex 本身中执行此操作。如何在 tex 或 latex 中定义新命令来解决这个问题?

答案1

您的要求并不十分明确,但我认为您想要的是这个,它可以无错误地运行。

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
function string(x)
tex.sprint(x)
end
function combine(x,y)
tex.sprint(x.." "..y)
end
\end{luacode*}
\newcommand{\test}[1]{\directlua{string(#1)}}
\newcommand{\othertest}[2]{\directlua{combine(#1,#2)}}
\test{"abc"}\\
\othertest{"abc"}{"def"}\\
\othertest{"\test{"abc"}"}{"\test{"def"}"}
\end{document}

"如果在宏定义中添加而不是在每次调用时添加,使用起来会更简单:

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
function string(x)
tex.sprint(x)
end
function combine(x,y)
tex.sprint(x.." "..y)
end
\end{luacode*}
\newcommand{\test}[1]{\directlua{string("\luaescapestring{#1}")}}
\newcommand{\othertest}[2]{\directlua{combine("\luaescapestring{#1}","\luaescapestring{#2}")}}
\test{abc}\\
\othertest{abc}{def}\\
\othertest{\test{abc}}{\test{def}}
\end{document}

这会从嵌套的加法和减法中打印出 7

\documentclass{article}

\begin{document}

\newcommand{\add}[2]{\directlua{tex.sprint(#1 + #2)}}
\newcommand{\subtract}[2]{\directlua{tex.sprint(#1 - #2)}}



% (2+(4-1))+2
\add{\add{2}{\subtract{4}{1}}}{2}

\end{document}

相关内容