LaTeX build server in Ruby

So — I’ve been writing functional specs at work in LaTeX, a system for writing documents in a code-y markup language. Seems to fit nicely with being in a programmer headspace. The only thing, really, is that you have to compile your document. A bit of a ballache. But — I’ve also been learning Ruby, and found it easy to knock up a program that sits patiently waiting for you to save your .tex source file, then recompiles it in the background.

So, if you want to use it, here’s how it works;

  1. modify the script below to point to your own latex distribution and source file.
  2. run the script to start the ‘build server’
  3. open your .tex file, make a change, and save it.
  4. watch your ruby program spring to life and compile to a .dvi file
  5. Open that .dvi file in a viewer; I’m using yap.
  6. make more changes to your .tex file. Every time you save it, the .dvi file will be updated, and yap will reload it, giving you instant feedback on your changes. It’s just like a wysiwyg editor! almost!

enjoy;

#
# LaTeX build server!
#
# Steve Cooper
#

file = "c:\\documents and settings\\steve\\Desktop\\bulk-activation-specification.tex"

cmd = "\"C:\\Program Files (x86)\\MiKTeX 2.7\\miktex\\bin\\latex.exe\" --interaction=nonstopmode \"" + file + "\""
print "Compilation command:\n  #{ cmd }\n\n"

class String
  def mtime
    File.new(self).mtime
  end
end

lastmtime = file.mtime

while true
  currentTime = file.mtime
  if (currentTime > lastmtime) then
    print "file modified at " + currentTime.to_s
    lastmtime = currentTime
    print "Compiling " + file + "\n"
    system cmd 
  end
  print ". "
  sleep 1
end

Leave a Reply