GeneralBatch compilation with incremented files

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
KarlG
Posts: 3
Joined: Tue Sep 08, 2020 2:59 am

Batch compilation with incremented files

Post by KarlG »

Hi all,
I'm a physics teacher and I'm using LaTeX for years, with Kile. Because of COVID, I'm having my students to do their exams from home. I have prepared 45 different graphs (graph1.pdf, graph2.pdf, graph3. pdf, etc) to be included in the exams. So exam1 will include graph1, exam2 will include graph2, etc. Questions are the same, and several questions with eventually other pictures (all pdf for quality) How can I bath-compile without doing everything manually? I searched google, but I didn't find a tip. Can anybody give me clue? Very appreciated!

I'm on Linux and Windows. Doesn't have to be with Kile, but I only know Kile and, a long time ago, TexNicCenter.

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Batch compilation with incremented files

Post by Ijon Tichy »

If everything but a running number is the same, you can make a master document using a kind of variable, e.g.:
% master.tex
\providecommand*{\RNo}{01}
\documentclass{article}
\usepackage{mwe}
\begin{document}
\section{Test image no. \RNo}
\includegraphics[width=\textwidth,height=\textheight,page=\RNo]{example-image-letter-numbered}
\end{document}
Now you need a simple script (here with bash syntax for Linux) to produce several documents from the master:
#! /bin/sh
for n in {1..10}
do
  printf -v RNo "%02d" $n
  pdflatex --jobname exam$RNo '\def\RNo{'$RNo'}\input master.tex'
  # add additional runs of pdflatex, makeindex, biber etc. here
done
If you now run the script, 10 files exam01.pdf … exam10.pdf with different images and section headlines will be generated.

The \providecommand*{\RNo}{01} is used to make it possible to test the master file directly with No. 01.

Something like the bash script can be implemented fpr each other script engine and even using texlua or tex itself. And you can use \RNo several times in you master, i. e., in filenames like graph\RNo.png.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
KarlG
Posts: 3
Joined: Tue Sep 08, 2020 2:59 am

Batch compilation with incremented files

Post by KarlG »

Great! And how do you do a script? I've never done that. I guess its in the terminal?
Sorry, I'm only an end user, but eager to learn!
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Batch compilation with incremented files

Post by Ijon Tichy »

This is more a linux question. First you have to make the script executable. This can be done in the file attributes (usually found via popup menu after right-click to the script file in the used file explorer, e.g., in dolphin if you are using KDE) or in a terminal using chmod u+x doall.sh. Then you can start a script like other applications from the file explorer.

Personally I always use a terminal for running scripts, because then I can see the terminal output of the script. Note, depending on your system you have to run scripts from the local directory with ./doall.sh instead of doall.sh.

And personally I would recommend to either delegate such a problem to someone with more experience in script programming or to read an instruction in bash programming / linux terminals. Better understanding of the command line is always useful (not only for linux users).
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
KarlG
Posts: 3
Joined: Tue Sep 08, 2020 2:59 am

Batch compilation with incremented files

Post by KarlG »

That works! Not that I doubted... It's a whole world of possibilities that opens to me.
And with the [page=] option that I didn't know, I have no need to burst my figures file into different files. I'm saving hundreds of files...

Many thanks, and have a great day!
Post Reply