8

Calling texlua file.lua on the command line runs the file through a Lua interpreter. Is it possible to give arguments to the script?

For instance, in

-- file.lua
io.write(3.4 + 5.6)
io.write("\n")

How do I make 5.6 into a parameter given on the command line?

  • When egreg and I wrote checkcites, we opted for running it under texlua because the interpreter is available on every modern TeX distribution. :) – Paulo Cereda Jul 31 '12 at 18:31
  • @PauloCereda My goal is to provide a couple of LuaTeX-like functionalities in XeTeX by calling a texlua script within \immediate\write18. – Bruno Le Floch Jul 31 '12 at 19:09
  • cool, the idea sounds awesome! :) Well, you are a synonymous of awesomeness. :) – Paulo Cereda Aug 1 '12 at 13:37
8
-- file.lua
io.write(arg[1] + arg[2])
io.write("\n")

$ texlua file.lua 103.4 5.6
109
  • 3
    Just to expand David's answer: Lua creates a table with all the command line arguments in a global variable named arg. The script name is in the index 0, the first argument is in 1, and so on. If you try negative indices, you go back in the line, so in David's example, arg[-1] == texlua. :) – Paulo Cereda Jul 31 '12 at 18:25
  • @PauloCereda But you can't go further back than texlua, i.e., in time texlua file.lua, arg[-2] is nil. – Bruno Le Floch Jul 31 '12 at 19:07
  • @BrunoLeFloch: you are right, the chain stops at the interpreter. :( – Paulo Cereda Aug 1 '12 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.