Search code examples
remacssweave

Exempt code chunks in an Sweave document from emacs spell check


In a Sweave document, code chunks in R are set off from the main text like so:

Catz are well known for their fur & pur. 

<<echo = false>>=
catz <- 1 + 2 
@

I'd like to run spell check for the LaTeX part (and flag "Catz") but have it skip the code chunks (not flag "catz"). In a long document, hitting "SPC" for each "misspelling" in the code section gets tedious.


Solution

  • Try adding this to your emacs init file:

    (add-to-list 'ispell-skip-region-alist '("^<<.*>>=" . "^@"))
    

    Edit (Re Michael Hoffman's comments):

    If Flyspell is enabled, these two additional expressions will also be needed:

    (defun flyspell-eligible ()
      (let ((p (point)))
        (save-excursion
          (cond ((re-search-backward (ispell-begin-skip-region-regexp) nil t)
                 (ispell-skip-region (match-string-no-properties 0))
                 (< (point) p))
                (t)))))
    
    (put 'latex-mode 'flyspell-mode-predicate 'flyspell-eligible)
    

    For other modes, replace the latex-mode in the last expression with the appropriate major mode names.