The following is the code.
\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}
This works fine. The following code also works well.
\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}
The following is another code which doesn't work.
\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}
I am working with some complex document where I want to use output of one lua function as input of another function in tex file itself. I know I can do this in lua itself. But I am working with something general. Say I have defined addition and subtraction functions for two numbers in lualatex. Now I want to use output of one function to the other. For example
\def\x{2}
\def\y{3}
\add{x}{y}
\subtract{x}{y}
This will work. However if I want to do
\subtract{\add{x}{y}}{y}
Here is what I mean.
\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}
This won't work. I know I can define another function in lualatex which will do both addition and subtraction. But as I mentioned, I want to do this in tex itself. How can new commands be defined in tex or latex to solve this?
\def\str\test{"abc"}
defines\str
to be a token that must always be followed by\test
or gives an error, and if it is followed by test it returnsabc
, I would guess that isn't the intended definition? similarly\othertest{\str}
is an error as you have defined it to require a\test
token as the next token.\subtract{\add{x}{y}}{y}
will not work but you give no example definition (I would expect that to work)string(x)
andcombine(x,y)
containtex.sprint
statements, which are instructions to print to the output document. If you want to collect the values for further processing instead of printing them directly, then you needreturn
statements in your functions."
adding.