0

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?

16
  • \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 returns abc , 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. Jul 25, 2019 at 15:18
  • 1
    you say \subtract{\add{x}{y}}{y} will not work but you give no example definition (I would expect that to work) Jul 25, 2019 at 15:21
  • Moreover, your definition of string(x) and combine(x,y) contain tex.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 need return statements in your functions.
    – Marijn
    Jul 25, 2019 at 15:26
  • @Marijn the arguments are fully expanded before being passed to lua so it isn't really code just a list of character tokens, only thing really missing is that (if you want Lua to see it as a string) it needs " adding. Jul 25, 2019 at 15:28
  • @DavidCarlisle I don't think the OP intended the arguments to be passed to Lua verbatim as a string, I think it was expected that the command was executed and the return value used as argument to the function. But I didn't realize that the arguments are fully expanded, so I'll remove my first comment.
    – Marijn
    Jul 25, 2019 at 15:32

1 Answer 1

2

Your requirements are not exactly clear but I think you want this, which runs without error.

\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}

It is simpler to use if you add the " in the macro definitions not on every call:

\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}

And this prints 7 from nested add and subtract

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .