# Auctex: how to cause math mode dollars to be closed automatically with electric-pair

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. - add comment ## 3 Answers I use cdlatex-mode for this (EmacsWiki, elisp). It has many other features to make typing easier (such as automatic insertion of paired \begin{environment} and \end{environment} for any environment, of paired \left and \right, typing a to get \alpha etc., and insertion of paired {} (with the cursor placed inside) every time you type _ or ^). It takes a while getting used to its usefulness, but it's very convenient once you get familiar. - (Sorry to bump up an old question, but I did a Google search trying to remember the name of the mode — I've lost my Emacs setup and it's been a few years since I typed a TeX document — and arrived at this question, so I thought it might be useful to mention it here.) – ShreevatsaR May 2 at 18:49 It should be noted that auctex already does this (to my knowledge). – Sean Allred May 2 at 20:43 @SeanAllred: No it doesn't, at least not by default. Looking for it now, I find there's a TeX-electric-sub-and-superscript user option for the subscript/superscript (not true by default), and there is some way for braces (you have to type C-c { instead of just {, which makes it not very useful), but it doesn't do most of the other things, including the \$ signs that this original question was about. –  ShreevatsaR May 3 at 2:31
I stand corrected. electric-sub-and-superscript was what I was thinking of at the time. –  Sean Allred May 3 at 12:46
(electric-pair-mode)
(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. - add comment For inserting stuff in LaTeX mode I use the following code in my .emacs: (defun brf-TeX-Inserting (sta stb stc num) " after entering stb insert stc and go back with the cursor by num positions. With prefix nothings gets replaced. If the previous char was sta nothing will be replaces as well." (if (null current-prefix-arg) (progn (if (= (preceding-char) sta ) (insert stb) (progn (insert stc) (backward-char num)))) (insert stb))) (defun brf-TeX-dollarm () (interactive) (brf-TeX-Inserting ?\\ "$"  "$$" 1)) (add-hook 'LaTeX-mode-hook (function (lambda () (local-set-key (kbd "") 'brf-TeX-dollarm))))  Then typing  inserts the pair of $$ and puts the cursor in between.
This isn't quite what I'm looking for, because when you have finished math mode, entering another $ should just move over the closing $ that was created automatically, just as electric-pair` gives you for the other kinds of brackets. –  robin Oct 8 '12 at 17:20