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.


In the end it was simpler than I thought.

(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.


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)
        (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

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 '13 at 18:49
  • It should be noted that auctex already does this (to my knowledge). – Sean Allred May 2 '13 at 20:43
  • 1
    @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 '13 at 2:31
  • I stand corrected. electric-sub-and-superscript was what I was thinking of at the time. – Sean Allred May 3 '13 at 12:46

I am using this one. It has the advantage that you can mark something and then typing $ will enclose it with dollars too. However it doesn't have the feature that entering another $ should moves over the closing $ that was created automatically.

(defun D-active-mark ()
    (and transient-mark-mode mark-active))

(defun  LaTeX-insert-dollar(arg)
  (interactive "P")
  (if (D-active-mark)
    (if (< (point) (mark))
    (insert "$")
      (goto-char (mark))
      (insert "$")))
    (insert "$")
      (if arg (forward-sexp (prefix-numeric-value arg)))
      (insert "$"))))

(add-hook 'LaTeX-mode-hook
          '(lambda ()
            (define-key LaTeX-mode-map "$" 'LaTeX-insert-dollar) ))
  • Development version of AUCTeX already has this feature and can automatically insert $...$ or \(...\) by typing just a single dollar. – giordano Jan 9 '14 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.