\begin{equation}\label{}
\end{equation} and in addition the cursor would go right
where it is supposed to be, in between the curly braces of \label{}? That is possible. We will need to
write a small emacs lisp function to insert the above and then
move the cursor back as many positions as needed to place it where it
should be. Then we will make the \eq
abbreviation call this function. Below I explain how to implement
all of this.
First, let us create a file where we will store all LaTeX-related
preferences and functions for XEmacs. And then we need to tell XEmacs
to load that file every time we edit a LaTeX file. We can for instance
name that file my-latex.el and store it in the directory
.xemacs. Then, paste the following in
init.el
; associate the files with the .tex extension with the LaTeX mode (setq auto-mode-alist (append '(("\\.tex$" . LaTeX-mode)) auto-mode-alist)) ; load the LaTeX mode (require 'tex-site) ; Tell XEmacs to load `my-latex.el' when opening LaTeX files (add-hook 'LaTeX-mode-hook '(lambda() (load-file "~/.xemacs/my-latex.el") ; load these LaTeX preferences )) |
(setq-default abbrev-mode t) ; enable abbreviations (setq save-abbrevs t) ; save abbreviations upon exiting xemacs (setq abbrev-file-name "~/.xemacs/my-abbreviations.el") ; the file storing the abbreviations (if (file-readable-p abbrev-file-name) ; read the abbreviations every (read-abbrev-file abbrev-file-name) ; time xemacs is started ) |
; Define the table of abbreviations in latex-mode. Note that we have ; to use '\\' to stand for backslash, since, just like in LaTeX, in ; emacs lisp the backslash character is special. It would have been ; logical for this table to be called latex-mode-abbrev-table, but for ; some reason that does not work. (define-abbrev-table 'text-mode-abbrev-table '( ("\\a" "\\alpha" nil 0) ; define \a to be an abbreviation for \alpha ("\\b" "\\beta" nil 0) ; and \b for \beta ("\\lra" "\\leftrightarrow" nil 0) ; and \rla for \leftrightarrow ; Other abbreviations go here. Note the closing parentheses below. ) ) |
("\\ci" "\\cite{}" backward-char 0)
|
; define an emacs lisp function to move the cursor back three characters (defun move-back-three-chars () ; the function is called 'move-back-three-chars' (interactive) ; all emacs lisp functions must have this line (backward-char 3) ; this function will do nothing but move the cursor 3 chars ) |
("\\fr" "\\frac{}{}" move-back-three-chars 0)
|
; an emacs lisp function to insert '\frac{}{}' and then move the cursor back three characters (defun my-latex-fraction () (interactive) (insert "\\frac{}{}") ; insert a piece of text (backward-char 3) ; move the cursor back three characters ) |
("\\fr" "" my-latex-fraction 0)
|
(defun my-latex-equation () ; a function to insert the equation environment (interactive) (insert "\\begin{equation}\\label{}\n") ; \n is the newline character (insert " \n") (insert "\\end{equation}") (previous-line 3) ; move back to the line containing \begin... (forward-char 24) ; move inside the curly brackets ) |
(defun smart-space () ; make the space key behave in a smarter way (interactive) (if (not (expand-abbrev)) ; test if the current word is an abbrev. If yes, expand it. (insert " ") ; If not, insert a plain space ) ; Now you know how to use 'if' in emacs lisp ) (local-set-key [(space)] 'smart-space) ; bind the 'smart-space' function to the 'space' key |
(local-set-key [(meta a)] 'define-mode-abbrev); define abbrevs on the fly with Alt-a
|
(local-set-key [(meta space)] 'dabbrev-expand) ; expand dinamic abbreviations with Alt-space
|
|
|
aoleg@math.umn.edu |
|
|
Updated: March 07, 2004 |