4

When typing operators in quantum mechanics, there is a lot of stuff like "\hat{...}" going on. But in math mode, typing apostrophe+^ only gives \hat inside math environment.

How can I modify the .emacs file to achieve automatically adding a pair of braces after the macro and put the cursor inside the braces, like \hat{cursor}, as Ctrl+ C Ctrl+ M frac Enter gives \frac{cursor}{}. btw it would be better if the key binding `+^ could be reserved.

I tried that is suggested in the post but have not had any luck.

Thanks a lot!

  • 1
    This has been fixed in AUCTeX with this commit. – giordano Jun 21 '14 at 15:02
  • @giordano, you are the man! btw, for convenience, can we directly install this version on some package list, like melpa or elpa? – davyjones Jun 21 '14 at 17:26
  • GNU ELPA uses stable versions of AUCTeX and I'm not aware of any other repository distributing development version of AUCTeX, sorry. You can only compile it yourself, if you feel like trying. – giordano Jun 21 '14 at 17:40
  • @giordano, pls forgive my ignorance, but is it ok if i just replace the old preview-latex.el file with the new one and bit compile it? it seems it is only file that's been modified. – davyjones Jun 22 '14 at 4:55
  • No, there have been too many changes since the release of the last stable version to be able to pick up a single file and compile it. – giordano Jun 22 '14 at 7:22
3

With the warning that I am no emacs expert, based on the post you linked to, the following works for me (I've bound it to C-^, which is undefined as far as I can see, as are C-|, and C-~, otherwise, but obviously the binding is a matter of taste):

(defun LaTeX-insert-hat ()
  "Insert \\hat{} and position point between the braces."
  (interactive)
  (progn
    (insert "\\hat{}")
    (backward-char)))

(eval-after-load 'latex 
   '(define-key LaTeX-mode-map (kbd "C-^") 'LaTeX-insert-hat))

The lisp of course goes in your .emacs. It's reasonably obvious what you have to change for any other operator.

If you want to bind the function to an apostrophe sequence available when LaTeX-math-mode is enabled (which is a minor mode on top of AUCTeX) then instead of the keybinding sequence above (for instance, your comments suggested M-^) try

(eval-after-load 'latex 
                '(define-key LaTeX-math-mode-map (kbd "` M-^") 'LaTeX-insert-hat))

Personally I think I'd rather bind to M-6 than M-^, since having to press backtick then shift-meta-6 is a bit of a stretch. The trick is to make sure that the keybinding is "free". Open up a latex file, place yourself in LaTeX math mode (C-c ~) and then C-h k. The minibuffer will ask you to enter the key you want to have described, and enter your preferred keysequence. If that comes up as "undefined" you are good to go! If it comes up with anything else, then you need to find a different sequence.

Or you could look at yasnippet, of course; or redefine LaTeX-math-hat, which is the AUCTeX function bound to [backtick]-^ (which doesn't in my version add braces).

(I should also have added: have you considered electric-pair-mode: in that minor mode, if you type one delimiter {, you will have a second one inserted automatically and the cursor placed between them. Some people hate it: but it's a more general solution to your issue, since it works for any commands, and although it costs you one extra keystroke, it still saves you one too, and makes sure you don't end up with unbalanced braces.)

  • This is amazing! but is it possible to bound it to something like `+M+^ with a leading apostrophe and followed by a combination. I tried stackoverflow.com/questions/7821648/… but did not work out. Thanks! – davyjones Jun 22 '14 at 4:50
  • I've updated, though you are now (over) stretching my grasp of AUCTeX! – Paul Stanley Jun 22 '14 at 6:51
  • Thanks a lot! Now I actually bind it to "` S-6" which works, while "` ^" does not. I am using electric-pair-mode, but there are indeed a LOT of hats going on in quantum physics! one more thing, do you happen to know how to use key bindings in LaTeX-math-list? the other post on stackoverflow completely failed. thanks again! – davyjones Jun 22 '14 at 9:12
  • I get the expected result from (setq LaTeX-math-list '(((?\M-^) LaTeX-insert-hat nil nil))), but I find the direct keybinding clearer: the list would be desirable if you wanted something to appear in the menu, but in this case I think you don't. – Paul Stanley Jun 22 '14 at 9:26
  • This is wonderful! I did not realize that the bracket outside the question mark is the crucial point! Thank you so so much! – davyjones Jun 22 '14 at 10:44

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.