5

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:

\begin{figure}[ht]
…
\end{figure}

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.

alper
  • 1,370
  • 1
  • 13
  • 35
  • I've needed to solve this for a while, your question made me think of a solution. Thanks! – Tyler Mar 01 '21 at 18:06

1 Answers1

4

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) (do-auto-fill)))

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

(defun my-tex-auto-fill () (auto-fill-mode) (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.

Tyler
  • 22,234
  • 1
  • 54
  • 94
  • Can we add tikzpicture (in between \begin{tikzpicture} and \end{tikzpicture}) along with figure? – alper Mar 04 '21 at 23:18
  • yes, just add it next to "figure", like '("figure" "tikzpicture") – Tyler Mar 05 '21 at 01:59
  • I have tried with example code under https://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 05 '21 at 10:03
  • Yes, it works for me – Tyler Mar 05 '21 at 17:11
  • Wouldn't it be easier to add an entry like ("figure" current-indentation) to LaTeX-indent-environment-list? Or am I missing something? – Arash Esbati Jul 01 '22 at 20:10
  • @ArashEsbati maybe. If you have a better answer, feel free to add it. I haven't looked at this in over a year, and don't use AucTeX regularly for a long time now. – Tyler Jul 06 '22 at 18:40