To write a bash script that reads a list of filenames from a file called filetexts.txt and then recursively compiles each of these files using a LaTeX compiler, follow these general steps:
- Read the filenames from
filetexts.txt.
- Iterate over each filename.
- Compile each LaTeX file.
Here's a sample bash script that accomplishes this using pdflatex as the LaTeX compiler:
#!/bin/bash
# Check if filetexts.txt exists
if [ ! -f filetexts.txt ]; then
echo "filetexts.txt not found!"
exit 1
fi
# Read the file line by line
while IFS= read -r filename; do
# Check if the file exists
if [ ! -f "$filename" ]; then
echo "File $filename not found, skipping..."
continue
fi
# Compile the LaTeX file recursively
echo "Compiling $filename..."
pdflatex -interaction=nonstopmode "$filename"
# Check if compilation was successful
if [ $? -eq 0 ]; then
echo "$filename compiled successfully."
else
echo "Error compiling $filename."
fi
# Run bibtex or biber if needed (optional, uncomment if using bibliography)
# bibtex "${filename%.*}"
# biber "${filename%.*}"
# Run pdflatex again to resolve references
pdflatex -interaction=nonstopmode "$filename"
pdflatex -interaction=nonstopmode "$filename"
done < filetexts.txt
Script Explanation:
-
Check if filetexts.txt exists:
- The script checks for the existence of
filetexts.txt. If not found, it prints a message and exits.
-
Read the file line by line:
while IFS= read -r filename; do ... done < filetexts.txt reads each line from filetexts.txt into the variable filename.
-
Check if each LaTeX file exists:
- For each filename read, the script checks if the file actually exists before attempting to compile it. If the file does not exist, it skips to the next filename.
-
Compile the LaTeX file using pdflatex:
- The
pdflatex -interaction=nonstopmode "$filename" command compiles the LaTeX file. The -interaction=nonstopmode flag makes LaTeX compile without stopping for user input on errors.
-
Optional Step: Bibliography Processing:
- If your LaTeX files include a bibliography, you may need to run
bibtex or biber after the first compilation and then run pdflatex twice more to resolve references.
Recursive Compilation:
LaTeX documents often need to be compiled multiple times to resolve references (\ref, \cite, etc.). Therefore, the script runs pdflatex multiple times.
How to Use the Script:
- Save the script to a file, for example
compile_latex.sh.
- Make it executable:
chmod +x compile_latex.sh
- Run the script:
./compile_latex.sh
Ensure that your filetexts.txt contains a list of LaTeX files you want to compile, one filename per line, e.g.:
document1.tex
document2.tex
path/to/document3.tex
This script provides basic error handling and assumes that pdflatex is installed and available in your environment. Adjust the script as needed for your specific requirements.