0

I've written a LuaLaTeX document that, when compiled, automatically generates a list of questions with answers for an upper-secondary class in quadratic equations. The details are messy, but the main relevant fact is that these are generated randomly: every time I compile, I get a new random pdf.

Now I want to compile one of these pdfs for every student, but pressing the compile button in TeXStudio 20 times then manually numbering the files main1.pdf,main2.pdf etc. gets pretty tedious. Is there any way I can automate this process?

For the sake of having a MWE, you can imagine something like

%!TeX program = lualatex
\documentclass{article}

\usepackage{random} \newcounter{\somenumber}

\begin{document} \setrannum{\somenumber}{1}{2} The number is \somenumber. \end{document}

Ideally a solution would be self-contained in the TeX file, but if it's not possible I'm open to alternatives (e.g. a bash script?).

  • In case anyone's curious, this is the same project as my previous post https://tex.stackexchange.com/q/702705/169442 – confusedandbemused Jan 24 '24 at 06:07
  • lualatex does understand option -jobname that can be used to change the basename of all output files, e.g., for n in {1..20};do lualatex -jobname student$n main.tex; done would create student1.pdf, student2.pdfstudent20.pdf from main.tex. latexmk also supports -jobname. So if you need several LaTeX runs + MakeIndex + Biblatex + …, you can also use latexmk inside the loop. – cabohah Jan 24 '24 at 06:55

1 Answers1

0

You can use -jobname option of lualatex (and also pdflatex or xelatex) to change the basename of all output files, not only the generated PDF but also the log-, aux- etc. files.

If you need more than one LaTeX run and maybe additional runs like biber or makeindex, you can also use latexmk instead of lualatex. latexmk also understands option -jobname. See the manual for more information.

How to loop over numbers using bash or cmd.exe or another shell is off-topic here. But as an alternative you can use the shell-escape feature of lualatex (or pdflatex or xelatex) to loop, e.g.:

% !TeX program = lualatex
\begin{filecontents*}[overwrite]{\jobname-document.tex}
\documentclass{article}

\begin{document} This is \texttt{\jobname.pdf}. \end{document} \end{filecontents*}

\documentclass{article} \usepackage{shellesc} \newcounter{subdocument} \makeatletter @whilenum \value{subdocument}<20\do {% \stepcounter{subdocument}% \ShellEscape{% latexmk -lualatex -jobname=\string"\jobname\thesubdocument\string" \string"\jobname-document\string" }% } \makeatother \begin{document} \ifnum \ShellEscapeStatus = 1 See the generated files \texttt{\jobname1.pdf} \dots{} \texttt{\jobname\thesubdocument.pdf}. \else Nothing generated. You have to use \begin{quote}\ttfamily lualatex -shell-escape \jobname\end{quote} to generate the files. \fi \end{document}

Note: You have to run lualatex with option -shell-escape.

cabohah
  • 11,455