9

When using flyspell in emacs, it can be useful to configure flyspell to ignore the contents of certain environments, e.g. tikzcd or tikzpicture.

For example, in the document below one would like to flyspell to ignore the word "asdf" inside the environment myenv.

\documentclass{standalone}

\newenvironment{myenv}{}{}

\begin{document}

\begin{myenv}
  asdf
\end{myenv}

\end{document}

How to achieve this?

lockstep
  • 250,273
Gustavo
  • 1,198

1 Answers1

7

Adding the following code to your ~/.emacs file tells flyspell to ignore the content of the environments myenv1, myenv2 and myenv3:

(put 'LaTeX-mode 'flyspell-mode-predicate 'auctex-mode-flyspell-skip-myenv)
(defun auctex-mode-flyspell-skip-myenv ()
  (save-excursion
    (widen)
    (let ((p (point))
          (count 0))
      (not (or (and (re-search-backward "\\\\begin{\\(myenv1\\|myenv2\\|myenv3\\)}" nil t)
                    (> p (point))
                    (or (not (re-search-forward "^\\\\end{\\(myenv1\\|myenv2\\|myenv3\\)}" nil t))
                        (< p (point))))
               (eq 1 (progn (while (re-search-backward "`" (line-beginning-position) t)
                              (setq count (1+ count)))
                            (- count (* 2 (/ count 2)))))))))
  )
(add-hook 'LaTeX-mode-hook (lambda () (setq flyspell-generic-check-word-predicate 
                        'auctex-mode-flyspell-skip-myenv)))

One can append environments as necessary in lines 7 and 9 simultaneously.

giordano
  • 8,486
Gustavo
  • 1,198
  • 2
    This answer was adapted from http://superuser.com/questions/345084/how-to-exclude-in-flyspell-mode-and-flyspell-buffer and http://tex.stackexchange.com/questions/61449/flyspell-emacs-and-aspell-seem-to-diverge/154535#154535 – Gustavo Jan 17 '14 at 10:19
  • Can this solution applied for the commented blocks? – alper Oct 30 '22 at 20:51