0

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.

%tikzmaster.tex
\documentclass[crop,tikz]{standalone}
\begin{document}
  \directlua{
    if not (arg == nil) then
      for i,v in pairs(arg) do
        if i == 2 then ?????.input("\string\\item[" .. i .. "])
      end
    end
  }
\end{document}

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?

4
  • 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. – David Carlisle Jun 11 '20 at 23:38
  • Off-topic: .cld is for ConTeXt, not LaTeX. – user193767 Jun 11 '20 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 Jun 12 '20 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 Jun 12 '20 at 9:05
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:

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. :)

3
  • I thought lua would make things easier, not more complicated. Sorry for your inconvenience. – Jay-Pi Jun 12 '20 at 15:50
  • No problem. It's conceptually possible, but possible not always means desirable. Entertaining, tho – user193767 Jun 12 '20 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 Jun 12 '20 at 16:25

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.