Here's a script I hacked together last night.

If you're writing something with lots of complicated diagrams (like my thesis, to pick an example at random) then you often have to go through several compile-view-edit cycles before any particular diagram looks right. This involves a lot of tedious waiting. The obvious way to speed it up is to try and reduce the time between making an edit and seeing the results.

Hence this script. The idea is that you can 'watch' a LaTeX document, so that whenever you save a change the  display is updated as quickly as possible to reflect the change.

The script is currently somewhat inflexible in its assumptions. It compiles .tex -> .dvi -> ps, and uses gv as the PostScript viewer. If other people are interested, I can certainly make it more configurable. That's why I'm posting it here.

The reason for the aparently-perverse choice of gv as the viewer is that ps2pdf is really, really slow, so if you want a fast cycle – and you're using PostScript specials – you need to view the DVI or PS directly. I don't know a DVI previewer that works with the PostScript specials I use, so that leaves PS. I don't know of anything else for the mac that can display PS as quickly as gv (I'd love to be enlightened here), and gv is very pleasant to use once you get used to its idiosyncratic interface.

Anyway, that means you'll need to have X11.app and gv installed for it to work. As I say, this can potentially be changed, but that's the current situation. You also need to have gv in your $PATH.

Apart from avoiding PS->PDF conversion, the main trick I use to speed up the update cycle is to build a custom TeX format file for the document. The preamble – i.e. anything before \begin{document} – is compiled into a special format when the file is watched. Thereafter, when the file is updated, the preamble is merely inspected to see whether it has changed. If it hasn't, the previously-generated format is used. Assuming you don't change the preamble very often, this is a big win if you load a lot of large packages. It halves the compile time of the document I'm currently working on.

Attached are two files:

Watch document.tmcommand
  A TextMate command that invokes the watcher. Copy this into your favourite bundle. (If you try to watch a file that's already being watched, you're given the option to stop watching it. Alternatively you can just close gv, and it will stop watching. Note that quitting TextMate will *not* stop the watcher.)

latex_watch.pl
  A Perl script that does the real work. This should be placed in the directory Support/bin inside the same bundle that you copied the command into. (You probably need to create that directory.)

If something doesn't work, you _should_ get a nice error dialog telling you what went wrong.

I'd really appreciate any feedback. I think that, with some more flexibility, something like this could be useful to a lot of people.

Robin