I want to process command line arguments directly for usage in lua code and subsequent latex. Looking for documentation I found no hints to tex-bindings for the \input command.

In this sample case I want to process multiple .tikz files by using lua.

    if not (arg == nil) then
      for i,v in pairs(arg) do
        if i == 2 then ?????.input("\string\\item[" .. i .. "])

used with lualatex tikzmaster.tex fig1.tikz

So far I found cld which looks kinda unrelated and the idea. I do assume that I need to save the input-file as string for processing by tex, but I did only find output examples to print things in the lualatex reference.
May this even be conceptual impossible?

  • 2
    you can use the same form as in classic tex lualatex '\def\zzz{fig1.tikz,fig2.tikz}\input tikzzmaster.tex' then you can iterat through \zzz using tex or lua as you wish. Commented Jun 11, 2020 at 23:38
  • Off-topic: .cld is for ConTeXt, not LaTeX.
    – user193767
    Commented Jun 11, 2020 at 23:47
  • I proposed a solution, but it doesn't use Lua. It doesn't make a lot of sense if you're gonna work with the command line
    – user193767
    Commented Jun 12, 2020 at 1:17
  • I've added a solution which processes your input, but it is still a mishmash of TeX and Lua. It's not impossible, but I don't see any practical advantage with respect to other methods of automated processing.
    – user193767
    Commented Jun 12, 2020 at 9:05

1 Answer 1


A first caveat emptor: we are supposing all relevant files are in the same directory

First approach: use a bash script

If you're using Linux, a shell script will do the job:


Adapting the idea suggested by David Carlisle, we write the following script. Note we add the -jobname option so we have different output files. Otherwise, it will iterate over the same file (Sorry, I'm not fluent in Bash, so feel free to correct me if there's a better way).

for i in $(seq 1 $1); do 
lualatex -jobname fig$i '\def\zzz{fig'"$i"'.tikz} \input tikzmaster.tex'


On the TeX side, you'll intercept the variable \zzz as a string and then you're gonna call the corresponding input file. Uhm, I was thinking about that and Lua doesn't do any work here

\input \zzz

And finally, the magic:

sh myscript.sh 4 # 4 is an argument.
#Change according to the number of files you want to process 

Your output files will be fig1.pdf, fig2.pdf, fig3.pdf, and fig4.pdf So, as your files are fig1.tikz or fig418.tikz, without leading zeroes or other format variations, the script will work.

You could actually use Lua because it does nice things, but it would only replace the function of a bash script in this case.

The disadvantage of this is that it only works with files whose names follow a specific pattern.

Second approach: actually using Lua

To emulate your example, we can do the following, using a helper myscript.tex


%!TEX program = lualatex
\directlua{toprocess = [[\zzz]]}
--We capture only strings with .tikz as suffix
for w in string.gmatch(toprocess, "[^,%s]+%.tikz") do
--We remove the suffix so it doesn't appear in PDF filenames
    w = string.gsub(w, "([^,%s]+)%.tikz", "%1")
    os.execute([[lualatex -jobname ]]..w..[[ '\def\zzz{]]..w..[[.tikz} \input tikzmaster.tex']])
Yay! % To avoid error messages. This actually does nothing.

Your tikzmaster.tex file will look like that:


\input \zzz

And your command:

lualatex --shell-escape '\def\zzz{fig1.tikz,fig2.tikz,one.tikz,another.tikz} \input myscript.tex'

Quite cumbersome, but still possible. Moreover, you can now use arbitrary .tikz files. There are another possibilities: you can even generate tikzmaster.tex via filecontents or read your directory content via io.popen to search all .tikz files in there, but it gets more and more troublesome. I'd opt for a bash script or another method. :)

  • I thought lua would make things easier, not more complicated. Sorry for your inconvenience.
    – Jay-Pi
    Commented Jun 12, 2020 at 15:50
  • No problem. It's conceptually possible, but possible not always means desirable. Entertaining, tho
    – user193767
    Commented Jun 12, 2020 at 16:22
  • Btw, Lua actually does things easier most of the time. Don't let an exception dissuade you to learn more about this fascinating language. :)
    – user193767
    Commented Jun 12, 2020 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .