Search code examples
remacselispsweave

Get Emacs to ignore contents of \Sexpr{} command in Sweave document to prevent incorrect $-based syntax highlighting


When editing an Sweave document in LaTeX (using the Noweb mode), Emacs knows to "ignore" code that is in <<>>= blocks. However, for interstitial \Sexpr{} blocks, this isn't the case. Given that R references by columns via '$' and LaTeX uses $ to set-off equations, these \Sexpr{} blocks often break the syntax highlighting, like so:

Emacs highlighting issue when using Sweave

I have a very rudimentary understanding the elisp & Emacs syntax highlighting, but my hope is that it might be possible to add something to .emacs that will disable any parsing/$ detection within \Sexpr{}'s.


Solution

  • I thought emacs with ESS has correct syntax highlighting for Sweave?

    Anyway, the easiest "fix" is to just not use the $ operator but [[ instead. For example:

    foo$p.value
    foo[['p.value']]
    

    Should give the same result. I think foo$p.value is just short for foo[["p.value",exact=FALSE]]