I have the following external lua script:
-- file mylua.lua
function test(input)
tex.sprint(input)
end
And the following lualatex document:
\documentclass{article}
\usepackage{luacode}
\directlua{dofile("mylua.lua")}
\begin{document}
This works: \directlua{test("Hello world!")}
But this doesn't: \directlua{test("\textit{Hello world!}")}
\end{document}
This is a simple example illustrating an error I get in a more complex code I'm writing. I moved my lua code to an external file, in order to avoid problems escaping characters. However, inevitably I have to use macros in my \directlua calls, and most of the times I cannot control what the macros are.
Is there any way of avoiding this problem?

test(), so I cannot manually add\string\every time I need it. – NVaughan Jul 31 '20 at 23:15\detokenize{...}around the whole thing and use\luaescapestring{...}to automatically double\and other needed Lua escapings, I just thought it more useful to do it by hand here to highlight what the issue is. Or you could use the luacode package functions. – David Carlisle Jul 31 '20 at 23:38luacodepackage, you could make use of the package's\luastringNmacro to accomplish what"\luaescapestring{\detokenize{...}}"does. – Mico Aug 01 '20 at 00:09\luaescapestring{\unexpanded{...}}(see also https://tex.stackexchange.com/questions/550673). Using\detokenizemight be a bad idea because this will add spaces, e.g.\textit{Hello}becomes\textit {Hello}. If the string is written back to TeX with the default catcode table, that is not a problem but if the string is processed in Lua this might lead to some confusion. – Henri Menke Aug 01 '20 at 04:19\directlua{print("\luaescapestring{\detokenize{\relax\relax{z}}}")}and\directlua{print("\luaescapestring{\unexpanded{\relax\relax{z}}}")}both have spaces after the\relaxso there are some differences but it isn't clear that one is always better than the other, generally\unexpandedis like having\noexpandon every token (which adds spaces when the tokens are written out) and\detokenizeis like having\stringon every token (which does not add spaces) – David Carlisle Aug 01 '20 at 09:20