1

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

$x+y$^,

where ^ denotes point. But instead I get

$x+y$^$$.

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?

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

1 Answer 1

3

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
          'fix-electric-pair-paired-delimiters-in-tex-mode)

(defun fix-electric-pair-paired-delimiters-in-tex-mode ()
  (add-function
   :around
   (local 'electric-pair-skip-self)
   (lambda (oldfun c)
     (pcase (electric-pair-syntax-info c)
       (`(,syntax ,_ ,_ ,_)
        (if (eq syntax ?$)
            (unwind-protect
                (progn
                  (delete-char -1)
                  (texmathp))
              (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 and acknowledge you have read our privacy policy.

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