Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert $$ and put the cursor between them in autopair-mode

Tags:

emacs

elisp

I often use AuCTeX to make my latex documents so i use the dollar $ sign a lot.

If autopair minor mode is enabled, when you insert a ( it automatically inserts a ) (there are some exceptions where it doesn't, for example, if you insert it after a \, but i like the exceptions) and put the cursor between them, so you have (<cursor>), now if you type a+b), you get (a+b) and not (a+b)).

I would like to have a similar behaviour with $ that works only in TeX-mode when i enable autopair-mode.

I've tried adding the followint to my .emacs:

(add-hook 'TeX-mode-hook
       #'(lambda ()
             (push '(?$ . ?$)
                    (getf autopair-extra-pairs :everywhere))))

but it does not work for some reason.

The problem is that you usually have a lot of expression $...$ and when you insert a single $, all the text before that $ gets a wrong coloration which is annoying and then after you close the initial $ the text after the initial $ gets fixed.

like image 413
Zero Avatar asked Apr 28 '12 21:04

Zero


2 Answers

Not sure about AuCTeX, but this works for latex-mode

(eval-after-load "tex-mode" '(modify-syntax-entry ?$ "\"" latex-mode-syntax-table))

Not sure what the right syntax-table is for AuCTeX.

like image 162
event_jr Avatar answered Oct 07 '22 17:10

event_jr


FYI, I have code on Github for autopairing dollar signs and \[\] in Latex.

https://github.com/tomathan/autopair-latex/blob/master/autopair-latex.el

Hope this helps!

like image 28
Tom Guo Avatar answered Oct 07 '22 17:10

Tom Guo