The following code is a simple example of passing arguments from TeX to Lua. Just passing the arguments directly (the version without \luastringN) gives an error.
(./luafunction.aux)./luafunction.lua:2: attempt to concatenate local 's' (a nil value)
stack traceback:
./luafunction.lua:2: in function 'joinstring'
[\directlua]:1: in main chunk.
\joinstring #1#2-> \directlua {joinstring(#1, #2)}
l.19 \joinstring{foo}{bar}
I noticed that other code used the macro \luastringN when passing arguments from TeX to Lua, so I did too. But this is completely Cargo Cult programming, because I have no idea why. The docunentation in the luacode manual isn't very enlightening. On section 1.2 on page 3 of the v1.2a manual where \luastringN is documented, it talks about escaping special characters. But there are no special characters in my example. And I don't understand what special handling is required to pass from TeX to Lua. Explanations appreciated - feel free to dumb it down.
Additionally, this answer to the question "LuaLaTeX set a path in lua" mentioned that one should use token.get_macro instead. The documentation on pg 10.6.4 of the luatex v 1.10 manual says:
The get_macro function can be used to get the content of a macro
but I'm not sure what that means. Also, I did a search in TeX SE for token.get_macro, and got two hits.
So is that a better choice, and if so, why? And what does the get_macro function do, exactly?
\documentclass{article}
\usepackage{luacode}
\usepackage{filecontents}
\begin{filecontents*}{luafunction2.lua}
function joinstring (s, t)
tex.sprint(s .. t)
end
\end{filecontents*}
\directlua{require "luafunction2.lua"}
\begin{document}
\newcommand\joinstring[2]
{
% \directlua{joinstring(#1, #2)}
\directlua{joinstring(\luastringN{#1}, \luastringN{#2})}
}
\joinstring{foo}{bar}
\end{document}
Chat room discussion starts around here


#1or whatever should be passed to Lua, you are overthinking things: is that the core question? – Joseph Wright May 25 '19 at 17:57\luastringNgives the error. I'll edit to clarify. – Faheem Mitha May 25 '19 at 17:58get_macrois in an entirely different area – Joseph Wright May 25 '19 at 18:02