0

So this is some elisp code I have in my init.el file:

(defun complete-dollar () 
  (interactive)
  (insert "$$")
  (backward-char 1))

(defun my-latex-keys ()
  "Modify keymaps by latex-mode"
  (local-set-key (kbd "$") 'complete-dollar))

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

Now, I open a tex file to test it. And when I type a $ it just gives me $ instead of $$ as expected from the above elisp code. This is the value for the major-mode variable in the buffer:

major-mode: latex-mode

Also the key $ is binded to autopair-insert-or-skip-paired-delimiter when I inspect it. Any reason on why it's not working ? (Also, if my elisp code isn't the correct way of doing this, I would love to know the recommended approach)

Edit: Ok, this made it to work:

(add-hook 'LaTeX-mode-hook 'sibi-latex-keys)

So my question is how to find which hook is the approriate one out of the following choices:

  • latex-mode-hook
  • LaTeX-mode-hook
  • tex-mode-hook
  • The one you selected is for the AUCTeX package which inherits the other modes from tex-mode.el and then changes stuff. The AUCTeX package is not a part of the basic Emacs installation. The basic installation of Emacs just comes with the library tex-mode.el. You can take a look at the tex-mode.el library by typing: M-x find-library RET tex-mode RET. The other two hooks are primarily used when not using the AUCTex package. – lawlist Apr 16 '15 at 23:59
  • @lawlist Thanks. Is there any formal way of finding out the proper hook from the current buffer using any variables or so ? – Sibi Apr 17 '15 at 0:01
  • 1
    @lawlist Thanks. In that case, can you post your comment as an answer ? – Sibi Apr 17 '15 at 0:05
  • 1
    After a brief grepping of the AUCTeX source code, it is not as straight-forward as I had thought in terms of what portions of the tex-mode.el library are still utilized by AUCTeX. Here is a link to the AUCTeX manual regarding the basic hooks used by that library: gnu.org/software/auctex/manual/auctex/Modes-and-Hooks.html I would need to spend additional time grepping and reviewing the source code in order to better understand what portions of the tex-mode.el library are utilized by the AUCTeX package. I'm not comfortable with my present limited knowledge to post an answer. – lawlist Apr 17 '15 at 0:24
  • 1
    Recent GNU AUCTeX versions already have the feature you are looking for. Have a look at the customize option TeX-electric-math. – Tassilo Horn Apr 17 '15 at 18:28

Your Answer

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

Browse other questions tagged or ask your own question.