Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Emacs from linebreaking in TeX inline math

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.
like image 915
Xlea Avatar asked Jun 11 '13 09:06

Xlea


2 Answers

Try:

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

Place it in your .emacs and restart emacs.

like image 89
mk1 Avatar answered Nov 01 '22 01:11

mk1


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))))
like image 21
Stefan Avatar answered Nov 01 '22 03:11

Stefan