# In LuaTeX, how can I escape text that will be inserted in LaTeX?

I wrote a Lua function that dumps data from a TXT file in my LaTeX document. The problem I have right now is that the data the TXT file is treated as LaTeX code so I need to escape special characters with backslashes and so on. Is there a way to treat the data from the TXT file as "plain text" without any LaTeX formatting?

My hunch is that either:

• There is a LaTeX macro for this. kindof like verbatim, but without displaying my text in typewriter font.
• There is ready-made LuaTeX macro or function for this. Basically the opposite of \luaescapestring
• I'll have to write down a special-character-escaping function by hand.

Source for test.tex:

\documentclass{article}
\directlua{dofile("test.lua")}
\begin{document}

\directlua{test("test.txt")}

\end{document}


Source for test.lua:

function test(filename)
local file = io.open(filename)
local s = file:read("*a")
tex.print(s)
file:close()
end


My current test.txt, which uses LaTeX formatting:

Hello \$\#\%!  My desired test.txt would be plain text and possibly contain any special characters: Hello$#%!


Use tex.print(-2,s) to get the desired output.
Background: the function tex.print() accepts different numbers of parameters. In the simplest variant, it accepts the string that should be output. It also accepts a "catcode table" as the first argument (a number). There are predefined catcode tables, and -2 is a catcode table where all special characters are not special anymore. Therefore tex.print(-2,"#") does not insert a "# with catcode 6" but "# with catcode 12". As you all know, 12 is the catcode for "other characters" which are harmles.