# Autocompile LaTeX doc when updated in Windows (via dropbox, in this case)

Goal: I'd like to be able to have a script running on my Windows computer that autogenerates the PDF of my LaTeX doc whenever the main .tex doc gets updated.

Use case is I am editing the .tex file via an iPad, and I want my main computer to compile the pdf so that I can open it in my Dropbox app on the iPad to see the results, whenever I update the .tex file via the iPad and dropbox synchronizes it.

My Problem: The main thing I know nothing about is how to get a script running on windows that can automatically do this. I could spend hours googling and learning about such things, but would rather get a suggestion by someone who knows a quick solution. In short, please don't just tell me a windows scripting language to learn. Show me how to do make this work.

For everyone's benefit, here's what I understand is needed to make this happen:

1. A working LaTeX implementation on the Windows machine (miktex is base install, TeXnicCenter is editor of choice when on Windows machine)
2. A (latex friendly) text editor on my iPad with integration to dropbox. (textastic for me)
3. A script that automatically runs on my Windows machine whenever the dropbox file gets updated. This is where I need suggestions.

Bonus points for an easy way to enable and disable the script from running.

-
Does this help? How to monitor a folder and trigger a command-line action when a file is created or edited? (superuser.com/questions/226828/…) –  Ethan Bolker Mar 3 '13 at 14:12

I'll throw a `Powershell` script in the ring! The following code checks all TeX files in a pre-defined directory and --if for a certain TeX file no matching PDF is found -- runs `pdflatex` on it. If however a PDF file with the same name is found, the `lastwritetime` is checked. If the TeX file is newer than the PDF, `pdflatex` is run as well.

``````\$path = "C:\Users\Uwe\Dropbox\autocompile\"
cd \$path

# run this script until infinity or the next Windows crash
while(\$true){

\$files = gci "*.tex"

# check all TeX files in the \$files array
foreach (\$i in \$files) {
# Name of the PDF file
\$pdfpath = \$path+ \$i.BaseName + ".pdf"

if (Test-Path -path \$pdfpath){
"A PDF for \$i exists"
\$pdf = gci \$pdfpath
if (\$i.LastWriteTime -gt \$pdf.LastWriteTime) {
"TeX file is newer, let's create the PDF!"
pdflatex \$i
}
else {
"but the PDF is newer, so no compilation is needed"
}

} else {
"No PDF found for \$i, let's create it!"
pdflatex \$i
}
}
# wait 60 seconds before your start over again
Start-Sleep -Seconds 60

}
``````

EDIT: Some more information on Powershell. It comes pre-installed with all Windows versions since Win 7, for Win XP and VISTA one can install it. It is a pretty awesome object-oriented scripting language, I work a lot with it in the office. To run this script find the executable for the Powershell IDE (which is called `Windows Powershell ISE`). Important: The default setting is that one cannot run saved Powershell scripts (due to security), one has to run the Powershell ISE (or the equivalent cmd line pendant, I rarely use) with administrator privileges and execute the `set-executionPolicy`commandlet (set-executionPolicy help). After this you're ready to save the code from above and run it. I can't give a full intro here, have a look at the MS help page MS help for POSH. If one's working with Windows, having a look at Powershell in general is a really good idea.

-
Since this is not TeX related, please add more information: How to save and how to execute? Add perhaps also the link Windows PowerShell - Wikipedia. –  Speravir Mar 3 '13 at 18:56
Great start Uwe! However, if pdflatex returns an error, it currently hangs in the script. Anyway to make it generate a PDF file with the error message and then gracefully exit pdflatex? That would make it TeX related too. –  ajo Mar 3 '13 at 19:08
Use as `pdflatex` parameter `-interaction=nonstopmode` –  Uwe Ziegenhagen Mar 3 '13 at 19:15
`-interaction=nonstopmode` works well enough for now. If you get something even better working that'd be great. Thanks! This is the type of answer I was hoping for. –  ajo Mar 3 '13 at 19:30
Steps to make this work on a windows 7 computer. 1) Find `powershell_ise.exe` on your computer (try in `C:\Windows\System32\WindowsPowerShell\v1.0\ `), right click and say "run as administrator" 2) Type `Set-ExecutionPolicy RemoteSigned` into the commmand line of the powershell_ise.exe program 3) Change the filepath in the above script to the one containing your .tex file. 4) To test, edit your .tex file, save it, but don't compile the PDF. Try running it in powershell_ise.exe. It should work (it did for me). –  ajo Mar 3 '13 at 19:31

Take a look at LaTeX Daemon:

http://william.famille-blum.org/software/latexdaemon/

It automatically recompiles the document if it has changed. It precompiles the preamble to speed up compilation. It won't hang on error.

-

Latexmk does in principle what you want. It looks for changed files and also monitors changes in included .tex files. It only requires perl and a working LaTeX installation.

It does not monitor a whole directory though, so this would require some tweaking to compile files that might just appear.

-

I have another way that works well under Linux. It uses the filesystem's `inotify` to track changes in files or even directories. Here's what I tried under Ubuntu:

1. Install `inotify-tools` using `sudo apt-get install inotify-tools`
2. Run the following line from the terminal to tell `inotify` to watch for any changes of the TeX-file (change the name if necessary). I use a simple `pdflatex` here, `make` or `arara` are of course fine as well.

while true; do inotifywait -e modify autocomp1.tex && pdflatex autocomp1.tex; done

Unfortunately it does not work with TeXworks' own document viewer, it does not recognize that the PDF has been modified externally.

-