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:
myscript.sh
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).
#!/bin/bash
for i in $(seq 1 $1); do
lualatex -jobname fig$i '\def\zzz{fig'"$i"'.tikz} \input tikzmaster.tex'
done
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
\documentclass[crop,tikz]{standalone}
\begin{document}
\input \zzz
\end{document}
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
myscript.tex
%!TEX program = lualatex
\documentclass{article}
\usepackage{shellesc}
\usepackage{luacode}
\directlua{toprocess = [[\zzz]]}
\begin{document}
\begin{luacode*}
--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']])
end
\end{luacode*}
Yay! % To avoid error messages. This actually does nothing.
\end{document}
Your tikzmaster.tex
file will look like that:
tikzmaster.tex
\documentclass[crop,tikz]{standalone}
\usepackage{luacode}
\begin{document}
\input \zzz
\end{document}
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. :)
lualatex '\def\zzz{fig1.tikz,fig2.tikz}\input tikzzmaster.tex'
then you can iterat through\zzz
using tex or lua as you wish.