Today's Random Photos:
René van Bevern
view out of a train window


Most of the time, the compilation of LaTeX code using Emacs' AUCTeX mode is cumbersome: multiple hits of Ctrl+C are needed to run LaTeX the correct number of times, to run BiBTex, to runing LaTeX again, and to finally view the document in a DVI viewer. The Rubber tool does all this automatically: it runs LaTeX the correct number of times, runs bibtex, makeindex, and autocompiles figures from xfig or metapost. Unfortunately, the DVI files generated by Rubber do not support DVI Source Specials, which allow to jump from a paragraph in the LaTeX source to the corresponding paragraph in the DVI viewer and vice versa. We will now setup Emacs so that on the press of the Ctrl+C Ctrl+A key combination, it will save the document, compile it with source specials, and open the current paragraph in a DVI viewer.

The first part is the following srclatex script, which can be put into /usr/local/bin/srclatex. It is actually a hack that calls latex -src on the basename of the document given as parameter. As Emacs runs rubber from the directory containing the file opened in the current buffer, this works. The hack is needed so that the source specials in the generated DVI file do not refer to the absolute path of the source document.

#!/bin/bash

TEXINPUTS=.:$TEXINPUTS
fname="$(echo $2 | perl -pe 's/\{.*\/(.*)\"\}/\{$1\}/')"
echo $fname > asdf.log
latex -src "\\nonstopmode$fname"

The second part is a Rubber module that calls srclatex. Create the file /usr/share/rubber/modules/srclatex.rub with the content

set program "srclatex"

Finally, add this to our ~/.emacs configuration file:

(defun save-rubber-reload ()
  (interactive)
  (if (buffer-modified-p) (save-buffer))
  (if (= 0 (call-process 
            "rubber" nil "*Rubber*" t "-m srclatex"
            buffer-file-name))
        (TeX-view)
      (view-buffer "*Rubber*")))

(add-hook 'LaTeX-mode-hook
   '(lambda ()
      (define-key LaTeX-mode-map (kbd "C-c C-a")
                  'save-rubber-reload)
      (TeX-source-specials-mode t)))

Now, if you press Ctrl+C Ctrl+A in a LaTeX buffer that has TeX-PDF-mode set to nil, your document should automatically compile everything as needed and Emacs should open the current paragraph in xdvi.

© 2010 René van Bevern. Last modified 2010-03-23.