10

How can I find out (in AUCTeX) if \frac{}{} and \bm{} have predefined keyboard shortcuts? I couldn't find these two.

4 Answers 4

6

I can't find anything like that in the auctex documentation, but if you'd like to have shortcuts for them, add the following lines to your .xemacs/init.el file (if you use xemacs) or your .emacs file (if you use emacs):

(defun insert-frac ()
  "We insert  \\frac{}{} and position point before the first right brace."
  (interactive)
  (progn
    (insert "\\frac{}{}")
    (backward-char)
    (backward-char)
    (backward-char)))
;;--------------------------------------------------------------------
(defun insert-bm ()
  "We insert  \\bm{} and position point before the right brace."
  (interactive)
  (progn
    (insert "\\bm{}")
    (backward-char)))
;--------------------------------------------------------------------
(global-set-key "\C-cf"   'insert-frac)
(global-set-key "\C-cb"   'insert-bm)

You can then insert \frac{}{} and put point inside the first pair of braces by typing Control-c f and you can insert \bm{} and put point inside the braces by typing Control-c b.

3
  • Dear Phil, thanks. I couldn't find anything in the manual, too. An alternative approach to yours is to define a snippet with yasnippet. Cheers, Marius Sep 24, 2011 at 6:28
  • @Marius: Yes, I've recently heard of yasnippet, and it looks interesting. I've had all sorts of things like this set up for years, though, so I haven't felt pressed to learn a new way. If I were starting now, I might use yasnippet instead. Best regards, Sep 24, 2011 at 20:22
  • 2
    FWIW you can use (backward-char 3), and you probably want (define-key LaTeX-mode-map ...) unless you want the binding in all modes (not just LaTeX). Oct 12, 2011 at 11:47
5

I suggest a snippet. You need less keys and do not block expensive hotkey sequences. After installing yasnippet create this snippet via menu

# -*- mode: snippet -*-
# name: frac
# key: /
# --
\frac{$1}{$2}

and save it in the snippet folder. The filename is just for your eyes. Now you type / TAB and get \frac{|}{} with cursor at |

You can find my complete configuration on github if you get stuck. emacs-config

1
  • That's exactly what I use, very useful!
    – sebastian
    Nov 22, 2012 at 13:22
4

In Auctex, you can just use C-c RET frac RET and it will do just what you requested (with the cursor in the proper place.

2
  • Is that any faster than \frac, though? To me, it seems not to be a shortcut.
    – dustin
    Sep 15, 2014 at 22:32
  • C-c RET frac RET is seven keystrokes. \frac{}{} C-b C-b C-b is twelve keystrokes. Even if you wait until after typing the numerator to close the first brace and after the denominator to close the second, it is nine keystrokes. So, yes. Sep 16, 2014 at 2:35
3

You can customize LaTeX-math-list. It takes a key and a string or function as well as some menu definitions. In LaTeX-math-mode (a minor mode), pressing LaTeX-math-abbrev-prefix (default "`") and the key you defined either inserts the string or runs the function. Thus adding (with Phil's definition of insert-frac)

(?f 'insert-frac "frac" "Constructs")

will make it so that `` fwill insert\frac{}{}with the cursor properly positioned. Of course you will no longer be able to insert\phithis way, but you can choose a better key thanf`.

Looking at LaTeX-math-default shows you all the defaults. By default there is an entry for frac, but no key is assigned to it. There is no default for \bm.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .