Prevent Emacs from linear conversion in embedded TeX math

I use Emacs in conjunction with AUCTeX to edit manuscripts with lots of built-in math calculations using $ . When autocompleted (for example, using Mq ), Emacs often violates these built-in mathematical environments in positions that impair readability (for example, in indexes, etc.).

Is there any way to tell Emacs to prefer to put the whole environment $…$ in a new line if this prevents a violation? More specifically, if a math violation occurs, the entire environment should be wrapped on a new line that cannot be broken.

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 lead to

 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. 
+7
source share
2 answers

Try:

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

Put it in .emacs and restart emacs.

+5
source

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 are inside a short math environment, for example. something like:

 (defun my-latex-fill-nobreak () (save-excursion (when (search-backward "$" (- (point) 20) t) ;; We found a nearby $. Now let see if it 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)))) 
+1
source

All Articles