ESS materials

"ESS is a GNU Emacs and XEmacs mode for interactive statistical programming and data analysis" (ESS)

Currently my configuration is as follows:

  • GNU Emacs 23.1.1 (i386-mingw-nt5.1.2600) of 2009-07-30 on SOFT-MJSAON
  • AucTeX 11.84
  • ESS 5.11
  • Miktex 2.8
  • latest version of R

One of the things I was missing from ESS & AucTeX is in relation to Sweaving. In particular, what I wanted to do was to be able to press C-c C-c inside a documentation chunk in a .Rnw file and be able to run Sweave, followed by LaTeX etc. While there is some support for sweaving etc in ESS I wanted some integration with AucTeX (and its options). As a result I came up with the following code (put it into your .emacs file). It works in AucTeX 11.84 - see below for a version that works with 11.83).

(setq TeX-file-extensions
      '("Snw" "Rnw" "nw" "tex" "sty" "cls" "ltx" "texi" "texinfo"))
(add-to-list 'auto-mode-alist '("\\.Rnw\\'" . Rnw-mode))
(add-to-list 'auto-mode-alist '("\\.Snw\\'" . Snw-mode))
(add-hook 'Rnw-mode-hook
 (lambda ()
  (add-to-list 'TeX-expand-list '("%rnw" file "Rnw" t) t)
  (add-to-list 'TeX-command-list
   '("Stangle" "R CMD Stangle %rnw"
     TeX-run-command nil (latex-mode) :help "Run Stangle") t)
  (add-to-list 'TeX-command-list
   '("Sweave" "R CMD Sweave %rnw"
     TeX-run-command nil (latex-mode) :help "Run Sweave") t)
  (add-to-list 'TeX-command-list
   '("LatexSweave" "%l %(mode) %s"
     TeX-run-TeX nil (latex-mode) :help "Run Latex after Sweave") t)
  (setq TeX-command-default "Sweave")))

For AucTeX 11.83 the above code may work (I haven't tested it), but I know that the following code snippet will). Simply replace the third last line above with the following line.

'("LatexSweave" "%l \"%(mode) \\input{%s}\""

The reason for this is explained in this short thread on the AucTeX mailing list. There was some further discussion about problems with the use of \\input{%s} (which is what I had originally) versus %s, which can be accessed here.

Update 28 Oct 2010: I ran into some trouble Sweaving files today. Turns out that something must have changed in the way that Sweaving works in relation to file names. In fact, this could have been the problem for a while (haven't had to Sweave much for a few months. Anyway, the fix is included in the code above, that is, for Stange and Sweave I have replaced "%s" with "%rnw", which is also defined in the above code (added to TeX-expand-list). Should all be good now.