Hi Tri,
The other builtin way to insert fraction is to use C-c RET
, which is bound, in LaTeX mode (AUCTeX), to TeX-insert-macro
, which is an interactive function. According to its help (C-h f TeX-insert-macro RET
),
TeX-insert-macro is an interactive compiled Lisp function in `tex.el'.
It is bound to C-c RET, <menu-bar> <LaTeX> <Macro...>.
(TeX-insert-macro SYMBOL)
Insert TeX macro SYMBOL with completion.
AUCTeX knows of some macros and may query for extra arguments, depending on
the value of `TeX-insert-macro-default-style' and whether `TeX-insert-macro'
is called with C-u.
In the case of fractions, the keystroke sequence is C-c RET frac RET
, which enters
\frac{.}{}
where I use a dot to indicate the position of the point (Emacs term for cursor) after calling the function. It's not very efficient for \frac
(well, it might be somewhat efficient if you hate manually typing in curly braces), but it's good to know the standard solution. (I personally hate unnecessary interactive commands.)
As I told you before, in Emacs you can always code up functions yourself. (Emacs Lisp is Turing complete, so yeah, you can do anything—for instance, Emacs comes bundled with several mail clients, several web browsers, several tty games, etc.) For instance, you could consider the following code to insert \frac
:
(defun TeX-insert-fraction()
"Insert \"frac{}{}\" and move point to after the first \"{\"."
(interactive)
(insert "\\frac{}{}")
(backward-char 3))
(add-hook 'TeX-mode-hook
'(lambda () (local-set-key (kbd "KEYBINDING")
'TeX-insert-fraction)))
And possibly another keybinding to move two points to the next curly braces. Same thing. (The canonical way is to use a prefix argument 2 to forward-char
, namely, C-u 2 C-f
; you certainly don't want to do that for moving two points right.)
By the way, KEYBINDING
in the code snippet above should be something like C-c C-f
. Check if a keybinding already exists by C-h c
in the corresponding mode. For instance, C-h c C-c C-f
in LaTeX-mode tells me C-c C-f runs the command TeX-font
. If you don't care about that TeX-font
(I do use it, though), simply overwrite it.
I've been discussing how to insert \frac
. However, I found myself using \frac
rare enough so I prefer typing it myself. Moreover, \frac
's, as opposed to macros for symbols, sort of structure your equations, so I find them not so intrusive as compared to e.g. \alpha + \beta + \gamma + \delta
(you can you TeX-fold-mode
to get pretty print right inside Emacs for a lot of symbols, but that's a whole new story). If I find some recurring macro or macro set tiring to type, I'd use an alias for that, you know, like \renewcommand{\a}{\alpha}
. One great thing about Emacs is Emacs allows you to auto insert, i.e., pre-fill a file according to a template at the time you creates it (not sure if there's a vim equivalent). For instance,
(require 'autoinsert)
(auto-insert-mode)
(setq auto-insert-directory "~/.templates")
(setq auto-insert-query nil)
(define-auto-insert "\\.tex\\'" "latex.tex")
automatically fills every one of your new FILENAME.tex
with contents from ~/.templates/latex.tex
. Auto insert mode is actually more powerful than this simple application. See Doc and Emacs Wiki for details.
So I could drop all those aliases in the template file, and carry them around without worrying about them in the future. (Currently I have some thirty heavily used aliases along with common \usepackage
's, \newtheorem
's, \title
, \author
, begin/end documents, etc. in my template. Pretty handy.) If you dislike extra aliases in every single source file, you could also write separate template files and \input
them in source files (I have a couple of these for discipline-specific aliases/special macro defs), but then you risk portability. Anyway, the point is I find aliases more convenient than keybindings for inserting full macros (I could type faster and won't get an Emacs pinky). You mileage might vary.
By the way, here are a bunch of LaTeX-mode configs in my .emacs
that I consider essential for everyone. You might find them useful.
;;; get information on loaded packages
;;; (which leads to better parsing)
(setq-default TeX-auto-save t)
(setq-default TeX-parse-self t)
;;; pdf mode
;;; (generate pdf output instead of dvi)
(setq-default TeX-PDF-mode t)
;;; rename default auto directory to ~/.auctex-auto
;;; (if you don't want to see an ./auto directory in every single
;;; source file directory, use this)
(setq-default TeX-auto-local "~/.auctex-auto")
;;; auto-fill-mode
;;; (automatic line break; you could set the variable 'fill-column' to
;;; adjust the number of columns -- default is 70)
(add-hook 'TeX-mode-hook 'auto-fill-mode)
;;; auto indent with RET
;;; (you don't need to constantly press TAB to get your indentation
;;; right)
(add-hook 'TeX-mode-hook
'(lambda () (local-set-key (kbd "RET") 'newline-and-indent)))
Sorry about the longish email. I'm proud of Emacs (as well as LaTeX), so I couldn't stop talking about it once I begin! ;)
Thanks,
Zhiming