Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I disable auto-fill-mode when editing equations?

I like auto-fill-mode in general, but I'd like to turn it off in specific situations,
e.g. between a \begin{equation} and \end{equation}
or \begin{equation*} and \end{equation*} or \begin{eqnarray} and \end{eqnarray*} etc.

I did some searching and found Suppress emacs auto-fill in a selected region which looked relevant, but I couldn't make it work.
Specifically, I tried changing %%% BEGIN NO FILL and %%% END NO FILL
to \begin{eq and \end{eq to no avail.

Thanks in advance!

share|improve this question

migrated from stackoverflow.com Aug 31 '12 at 10:28

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 5 down vote accepted

This is a solution if you are using AUCTeX (which I highly recommend). Put the following code in your .emacs file:

(defvar my-LaTeX-no-autofill-environments
  '("equation" "equation*")
  "A list of LaTeX environment names in which `auto-fill-mode' should be inhibited.")

(defun my-LaTeX-auto-fill-function ()
  "This function checks whether point is currently inside one of
the LaTeX environments listed in
`my-LaTeX-no-autofill-environments'. If so, it inhibits automatic
filling of the current paragraph."
  (let ((do-auto-fill t)
        (current-environment "")
        (level 0))
    (while (and do-auto-fill (not (string= current-environment "document")))
      (setq level (1+ level)
            current-environment (LaTeX-current-environment level)
            do-auto-fill (not (member current-environment my-LaTeX-no-autofill-environments))))
    (when do-auto-fill

(defun my-LaTeX-setup-auto-fill ()
  "This function turns on auto-fill-mode and sets the function
used to fill a paragraph to `my-LaTeX-auto-fill-function'."
  (setq auto-fill-function 'my-LaTeX-auto-fill-function))

(add-hook 'LaTeX-mode-hook 'my-LaTeX-setup-auto-fill)

Let's go from the bottom to the top to understand what this does. The "add-hook" function registers a call-back that is invoked every time you open a .tex file. The hook variable LaTeX-mode-hook is provided by AUCTeX.

The call-back is the function my-LaTeX-setup-auto-fill which we define above. It turns on auto-fill-mode and then tells it to use the function my-LaTeX-auto-fill-function to actually perform the automatic filling.

In order for this construction to work, it is important that you don't turn auto-fill mode off an on again afterwards, or else the my-LaTeX-auto-fill-function won't be used. If you already have a mechanism in place that automatically turns on auto-fill-mode when you edit a .tex file, you should probably turn that off and have the above function turn it on for you instead.

The definition of my-LaTeX-auto-fill-function can be found above. What it basically does is this: it checks whether you're currently inside an "equation" environment and if so, it does nothing. If you're not inside an "equation" environment, it does auto-fill.

Finally, the variable at the top defines for which LaTeX environments auto-fill should be inhibited. I currently put "equation" and "equation*" there, but you can add further environments if you like.

share|improve this answer
That is absolutely perfect. Thanks! –  Michael Lerner Jul 14 '12 at 2:56
There's a small typo in the documentation string of my-LaTeX-auto-fill-function: the environments are listed in my-LaTeX-no-autofill-environments, not my-LaTeX-auto-fill-function. –  Michael Lerner Jul 14 '12 at 20:25
Fixed, thanks (damn auto-completion...) –  Thomas Jul 15 '12 at 1:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.