1

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?

4
  • Maybe something similar to tex.stackexchange.com/a/1495/89417? Commented Sep 15, 2017 at 21:59
  • Followed by, e.g., tex.stackexchange.com/questions/198628/… Commented Sep 15, 2017 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? Commented Sep 16, 2017 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. Commented Sep 16, 2017 at 12:25

1 Answer 1

5

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.

%argtest.tex \documentclass{article} \begin{document} \noindent Command line arguments: \directlua{ if arg then for i,v in ipairs(arg) do if (i > 1) then tex.print("\string\\begin{description}") tex.print("\string\\item[" .. i .. "]" .. v) tex.print("\string\\end{description}") end end end } \end{document} 

Invoked with

lualatex argtest.tex ham spam jam 

you obtain

enter image description here

2
  • 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). Commented Sep 15, 2017 at 22:45
  • 2
    @HenriMenke - thanks, updated. (The test is if arg, and it does work.) Commented Sep 15, 2017 at 23:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.