I am working on tex files with LaTeX-mode. If possible, I want to use auto-fill-mode in some conditions. I was wondering is it possible to disable it in between some patterns like:


where I want to apply auto-fill-mode only for pure text? and not apply it under \begin{figure} ... \end{figure} or any matematical formula notations.

  • I've needed to solve this for a while, your question made me think of a solution. Thanks! – Tyler Mar 1 at 18:06
  • @Tyler I ask too many questions in emacs forum, I am glad my question helped out :-) Thanks for your solution – alper Mar 2 at 16:20

You can do this by defining your own auto-fill-function, and configuring LaTeX mode to use that:

;; define environments we don't want to autofill:
(defvar unfillable-envs '("figure"))

;; add more environments if you like, eg:
;; (defvar unfillable-envs '("figure" "tikzpicture"))

;; create a function to check the environment first, then fill:
(defun my-filtered-fill ()
  (unless (member (LaTeX-current-environment) unfillable-envs)

;; setup LaTeX/AucTex to use auto-fill-mode, but with our fill-function:

(defun my-tex-auto-fill ()
  (setq auto-fill-function 'my-filtered-fill))

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

Note that you can add different environments to unfillable-envs if you want to block filling in other places.

You could accomplish the same thing by advising the fill function, but that can lead to unexpected problems if you're not careful.

  • Can we add tikzpicture (in between \begin{tikzpicture} and \end{tikzpicture}) along with figure? – alper Mar 4 at 23:18
  • yes, just add it next to "figure", like '("figure" "tikzpicture") – Tyler Mar 5 at 1:59
  • I have tried with example code under tex.stackexchange.com/a/585898/127048 but it didn't work :-( When I add a character to a line it applies auto-fill-mode, does it work on your end? – alper Mar 5 at 10:03
  • Yes, it works for me – Tyler Mar 5 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.