Most of the LaTeX users, especially who use pdflatex, keep a PDF viewer running to see changes while editing and compiling a LaTeX file. Also, they often use Make or SCons to automate the compile process.
In my case, I use SCons and it works well in Linux and Mac OS X. However, in Windows, SCons produces an error and is failed to update the PDF file when it is already opened by a PDF viewer. This is because most PDF viewers for Windows set a read-only attribute on an opened file and SCons removes a target file before build it as default. In other words, SCons would stop because it is simply failed to remove the locked file.
Other SCons users already experienced the same issue. Luckily, I find this “[scons-users] pdflatex preview on windows – file locking” thread. They examined an exact cause of this issue and suggested a very useful soultion using SCons’s the Precious Function which prevents target files from being removed.
Here is my wrap-up and solution to this issue.
import os
env=Environment()
# Look in standard directory ~/texmf for .sty files
env['ENV']['TEXMFHOME'] = os.path.join(os.environ['HOME'],'texmf')
env.PDF(target = 'main.pdf', source = 'main.tex')
env.Precious('main.pdf')
Lastly, I have an advice in choosing a PDF viewer. Do not use Adobe Reader. It prevents opened files from changing, so it ends up causing an error. At least, you need to use Foxit Reader, Sumatra PDF, Evince or so on. I prefer to Sumatra PDF because it reloads automatically when it detects a file changed.
Happy LaTeXing!



