Question

I use Emacs in combination with AUCTeX to edit manuscripts with a lot of inline maths using $. When automatic filling (e.g., with M-q), Emacs often breaks these inline math environments at positions which disturbs the fluency of reading (e.g., in subscripts or the like).

Is there a way to tell Emacs to prefer putting the whole $…$ environment in a new line if that would prevent breaking? More specifically, if breaking in maths would occur, the whole environment should be moved to a new line which should not be broken apart.

An example:

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed $a^2 + b^2 = c^2$ eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.

should result in

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed
$a^2 + b^2 = c^2$ eiusmod tempor incidunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam.
Was it helpful?

Solution

Try:

(add-hook 'LaTeX-mode-hook
          (lambda ()
            (add-to-list 'fill-nobreak-predicate 'texmathp)))

Place it in your .emacs and restart emacs.

OTHER TIPS

You can do something like:

(add-hook 'LaTeX-mode-hook
          (lambda ()
            (add-hook 'fill-nobreak-predicate 'my-latex-fill-nobreak)))

and then define a function my-latex-fill-nobreak which returns non-nil if you're inside a short math environment, e.g. something like:

(defun my-latex-fill-nobreak ()
  (save-excursion
    (when (search-backward "$" (- (point) 20) t)
      ;; We found a nearby $.  Now let's see if it's an opening $
      ;; rather than a closing one.  Using font-lock properties is
      ;; a hack, but it might be good enough.  Apparently (texmathp)
      ;; could be a better choice.
      (eq (get-text-property (point) 'face) 'font-latex-math-face))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top