2

This is a question a friend of mine had; figured posting here might give quick responses.

Old version in new language, après Name

How do I define the mapping ^ |--> ^ while ^^ |--> ^{}. Similarly, for _.

Old Version

Is there a way to modify the electric braces for superscript and subscript to behave as follows:

  • Insert no empty pair of braces when a carat ^ or underscore _ is entered.

  • Insert a empty pair of braces when a carat ^ (resp. underscore _) is entered following a ^ carat (resp. _ underscore) sign.

Any answer would be helpful.

/p/s/ The motivation is to minimise having to jump out of braces when the subscript or superscript is just really a character long.

kan
  • 123
  • 5

1 Answers1

3

This does what you asked for. It also put the cursor between the braces.

(add-hook 'LaTeX-mode-hook 'my-latex-hook)

(defun my-latex-hook ()
  (require 'tex-site)
  (define-key LaTeX-mode-map "^" 
    (lambda () 
      (interactive) 
      (if (equal (preceding-char) ?^) 
          (progn (insert "{}")(backward-char)) 
        (insert "^")))))

The credit is due to @abo-abo for his/her answer https://emacs.stackexchange.com/a/3882/2609 from which I got this idea.

Name
  • 7,689
  • 4
  • 38
  • 84