20090917

Quick latex-ing with emacs: dabbrev and skeletons

Althouh I use AucTeX, which already has nice quick-writing techniques, I have found emacs' abbrev-mode together with skeletons are a nice addition to it, allowing me to be really quick at writing LaTeX. The included examples to use dabbrev and skeletons are for the mathbb and theorem environments.

Sample usage: When I write \mbb, and then open the left {, mbb gets expanded to mathbb... so I have \mathbb{ as needed. The other example, I write \bth, press space and get a nice \begin{theorem} \end{theorem} delimiters with the cursor positioned between them.

This should be in ~/Abbrev.el
(define-abbrev-table 'text-mode-abbrev-table '(
("mbb" "mathbb" nil 0)
("bth" "" latex-skel-thm 0)
...
))
The three dots are just to indicate my file is much, much longer. The following should be in a file ~/Skeletons.el
(define-skeleton latex-skel-thm
"Insert a \\begin{theorem} environment" nil
"begin{theorem}" \n
> _ ?\n
"\\end{theorem}")
And finally, in your .emacs you should add the following lines:

(load "~/Skeletons.el")
(setq abbrev-file-name "~/Abbrev.el")
(add-hook 'text-mode-hook (lambda () (abbrev-mode 1)))
A suggestion from a friend, using the ç and Ç to add {} and \[\] with the cursor positioned in between. Done via keyboard macros:
(fset 'braces
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([123 125 left] 0 "%d")) arg)))

(global-set-key (kbd "ç") 'braces)

(fset 'displaymode
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([13 92 91 13 13 92 93 up] 0 "%d")) arg)))

(global-set-key (kbd "Ç") 'displaymode)
Although I may use the ç key (I'm a catalan speaker), in emacs I write generally LaTeX, in english... so I don't really have a use for it. I may change the "global-set-key" to set it just for LaTeX... or even create a minor mode for it (Non-Catalan-Keyb?).

What speed-up keyboard tricks do you have in your .emacs file? Share them!

Related posts:
Written by Ruben Berenguel