I am using LuaLaTeX with LuaSQL and I have two files which are test.tex and test.lua. When I compile with lualatex test.tex, first test.lua is ran and it reads user values from my database for rows where variable id_user = 3. This variable is set inside test.lua.

So currently every compilation returns values for same user, but I would like to pass user ID at compile time as an argument, like:

lualatex test.tex 2399

This would be for user with id_user = 2399. So the problem here is that I have to pass values to lualatex and not lua itself! How can this be done? Can anyone provide a MWE?

  • Maybe something similar to tex.stackexchange.com/a/1495/89417? – Marijn Sep 15 '17 at 21:59
  • Followed by, e.g., tex.stackexchange.com/questions/198628/… – Marijn Sep 15 '17 at 22:01
  • You're supposed to provide the MWE, so that people can modify it to solve the problem. People aren't supposed to need to guess what might or might not be in your code. It isn't clear to me what is where. What triggers the lua script? How can that be read first when you just do lualatex doc.tex. Surely, the .tex file processing must begin first? – cfr Sep 16 '17 at 3:28
  • 1
    I would not have marked this as a duplicate. The top-ranked answers on that other page don't specifically deal with LuaTeX, and their proposed solutions are quite different from this one. The LuaTeX-specific answer is found well down the page - obviously so far down that OP did not find it. – Michael Palmer Sep 16 '17 at 12:25

Recycled with minor changes from Sharpie's answer to a similar question Passing parameters to a document

Within your lua code, the command line arguments are available in arg. Here, we just print the arguments, but the code does show how do get hold of them.



\noindent Command line arguments: 

    if arg then
      for i,v in ipairs(arg) do
        if (i > 1) then
            tex.print("\string\\item[" .. i .. "]" .. v)


Invoked with

lualatex argtest.tex ham spam jam

you obtain

enter image description here

  • 2
    You can say if not arg then (nil evaluates to false in this context) and please use ipairs to iterate in ascending order (pairs may iterate in any order, usually order of insertion). – Henri Menke Sep 15 '17 at 22:45
  • 2
    @HenriMenke - thanks, updated. (The test is if arg, and it does work.) – Michael Palmer Sep 15 '17 at 23:00

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