[Tex/LaTex] Auctex: how to cause math mode dollars to be closed automatically with electric-pair

auctexemacs

I use electric-pair mode to cause brackets to be automatically closed for me, so that when I enter (, { or [, the closing bracket is automatically also entered into the buffer and the cursor is placed between them. However, I cannot seem to get it to treat $ the same way. Perhaps it is related to the fact that TeX-insert-dollar is bound to $, and that presumably electric-pair doesn't treat $ the same way as (. I've tried fiddling along these lines, but I can't get the behaviour I want.

Best Answer

In the end it was simpler than I thought.

(electric-pair-mode)
(add-hook 'LaTeX-mode-hook
          '(lambda ()
            (define-key LaTeX-mode-map (kbd "$") 'self-insert-command)))

This just removes the TeX-insert-dollar binding to $ and somehow (electric-pair-mode) takes over and realises that $ is a bracket in this context.