Make a file (say foo.lua
) containing your Lua code:
function printeverything()
data = {"a", "b", "c"}
for i = 1, #data do
tex.print(data[i])
end
end
Then in your .tex
file you can use it:
\documentclass{article}
\directlua{dofile('foo.lua')}
\newcommand{\printdata}{\directlua{printeverything()}}
\begin{document}
\printdata
\end{document}
(You don't need to use a Lua function — you could just dump the function's contents in your file and define \newcommand{\printdata}{\directlua{dofile('foo.lua')}}
— but if you have multiple chunks of Lua code you may not want to create separate files for each of them.)
Explanation: When working with Lua code, and putting it in your .tex
files, sooner or later you'll run into this issue: the TeX engine (LuaTeX) will read through the characters of your .tex
file assuming certain category codes, and characters like #
have special meanings in TeX that you don't intend in Lua code. There exists a way of working around this (run texdoc luacode
to see the documentation of the luacode
package), but IMO there are good reasons to do avoid these workarounds. As that documentation says in the very first paragraph:
Before presenting the tools in this package, let me insist that the best way to manage a nontrivial piece of Lua code is probably to use an external file and source it from Lua, as explained in the cited document.
The cited document (lualatex-doc.pdf
) says:
The luacode
package provides commands and environments that help to varying degrees with some of these problems. However, for everything but trivial pieces of Lua code, it is wiser to use an external file containing only Lua code defining functions, then load it and use its functions. For example:
\directlua{dofile("my-lua-functions.lua")}
\newcommand*{\greatmacro}[2]{%
\directlua{my_great_function("\luatexluaescapestring{#1}", #2)}}
One of the reasons I prefer putting Lua code in a separate .lua
file is that your editor probably does better syntax highlighting for the Lua code when it's in an separate .lua
file. (Similarly, you can get better highlighting when pasting the Lua code here on this website, if it's a separate code block.)
luacode
environment, but in general I strongly recommend putting lua code in separate files and including withdofile
. – ShreevatsaR Oct 25 '17 at 0:02#
in#data
", is somewhat misleading. The issue is that the argument of\directlua
is expanded (in the TeX sense of the word) before the Lua code is passed to Lua for further processing. And, because "#data
" is not a valid expression according to TeX's syntax rules, you get a TeX-based error message,! Illegal parameter number...
. – Mico Oct 25 '17 at 4:53