I'm trying to turn $...$ into an electric pair, just as say (...). So I tried this solution, and it works fine on my older machine, running Emacs 24.3.1 and AUCTeX 11.87. However, on a newer installation (Emacs 24.5.1, AUCTeX 12.1.1), it doesn't quite behave like it should. When I type $x+y$, it should be giving me


where ^ denotes point. But instead I get


Obviously, Emacs fails to move over the automatically-inserted closing $ and instead inserts a new pair. Here is the relevant part of my .emacs:

(electric-pair-mode 1)
(define-key LaTeX-mode-map (kbd "$") 'self-insert-command)
(push '(?\$ . ?\$) electric-pair-pairs)

The push line may be included or not, it doesn't make a difference. I also tried the following .emacs, as suggested by the AUCTeX manual:

(electric-pair-mode 1)
(set (make-variable-buffer-local 'TeX-electric-math) (cons "$" "$"))

This gives me $x+y$^$, which is not much better. How do I get the behavior I want?

  • 1
    I'm electric-pair-mode's author. This seems to be a bug. Can you report it via M-x report-emacs-bug? Jul 5 '18 at 9:41
  • I just reported it as you suggested.
    – pgraf
    Jul 6 '18 at 23:19

As I explained in my comment, this is a bug. Until it is fixed, you can work around it like so

(add-hook 'LaTeX-mode-hook

(defun fix-electric-pair-paired-delimiters-in-tex-mode ()
   (local 'electric-pair-skip-self)
   (lambda (oldfun c)
     (pcase (electric-pair-syntax-info c)
       (`(,syntax ,_ ,_ ,_)
        (if (eq syntax ?$)
                  (delete-char -1)
              (insert-char c))
          (funcall oldfun c)))))
   '((name . fix-electric-pair-paired-delimiters-in-tex-mode))))

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.