Is it easy to do the following script? I have my LaTeX-documents on /home/jaakko/Documents/LaTeX and its subdirectories. I would like to have a script watch the .tex documents in that directory and its subdirectories and if some tex-file changes, the script tries to compile it as soon as possible (pdflatex file.tex). How can I do that in Bash?
Latex Automation – Compile Latex Source Automatically on Creation or Change
latex
Best Answer
First of all, what you're trying to do is the usual job of an IDE. Whether it's Texmaker, Latexila, or another one, IDEs wil allow you recompile LaTeX code extremely fast, and some may permit automatic recompiling at given intervals.
Many IDEs nowadays rely on
inotify
(which is a C API) to detect file changes. However, the number ofinotify
watches is limited by the system configuration, and well... I took the challenge of writing an actual bash script doing the job.Anyway, here's a little idea using
find
and MD5 hashes :find
to find all.tex
files.update_file
) which checks whether the file has changed since last time, and callspdflatex
if necessary.md5sum
changes. Each file can be associated with a MD5 hash (obtained throughmd5sum file
). If the file contents change, so does the hash. Therefore, I can monitor file changes by monitoring MD5 hashes changes.Basically, I am using a
md5.sum
file to store all MD5 hashes associated with TeX files. When a file is modified, its hash changes, and therefore, is no longer the same as in the MD5 file. When this happens, the script callspdflatex
and updates the new MD5 hash.Here is the code, I added some information in the comments. Feel free to adjust it, and change the variables set in the beginning. However, always use absolute paths.
Now, if you want to run this script periodically, you can use
watch
:You may use the
-n
switch to adjust the refresh time:You can place this script in
/home/jaakko/Documents/LaTeX
and run it whenever you're developing.