# emacs auctex: how to get \frac{}{} and \bm{}?

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

-

## 4 Answers

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.

-
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 –  Marius Hofert Sep 24 '11 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, –  Phil Hirschhorn Sep 24 '11 at 20:22
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). –  Ivan Andrus Oct 12 '11 at 11:47

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

-
That's exactly what I use, very useful! –  sebastian Nov 22 '12 at 13:22

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.

-
Is that any faster than \frac, though? To me, it seems not to be a shortcut. –  dustin Sep 15 '14 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. –  Sam Grondahl Sep 16 '14 at 2:35

You can customize LaTeX-math-list. It takes a key and a string or function as well as some menu definitions. Then after pressing a prefix key (defined by LaTeX-math-abbrev-prefix and by defualt "") and the key you defined it 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  f will insert \frac{}{} with the cursor properly positioned. Of course you will no longer be able to insert \phi this way, but you can choose a better key than f.

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.

-