[Tex/LaTex] How to streamline insertion of LaTeX-math-mode symbols in AUCTeX

auctexemacsmath-mode

In AUCTeX, you can enable a minor mode called LaTeX-math-mode that instructs the prefix character ` to insert various common macros.
For example, ` t inserts \tau@ when you are in a math environment (where @ is where point is left).

This is wonderful when you are in a math environment already.
However, I often refer to these symbols in the running text, and just doing the same ` t inserts just \tau{}@ in text mode.

How can I instruct AUCTeX to, when in text mode, enter math mode like so: ` t yields $\tau$@?

Best Answer

Of course AUCTeX is already able to do this out of the box! From the description of LaTeX-math-mode (C-h f LaTeX-math-mode RET):

Easy insertion of LaTeX math symbols. If you give a prefix argument, the symbols will be surrounded by dollar signs.

Thus, C-u ` t inserts

$\tau$

If you want to automatically wrap dollars around the symbol when in text mode without the prefix argument, add this to your .emacs:

(add-hook
 'LaTeX-mode-hook
 (lambda ()
   (let ((math (reverse (append LaTeX-math-list LaTeX-math-default))))
     (while math
       (let ((entry (car math))
         value)
     (setq math (cdr math))
     (if (listp (cdr entry))
         (setq value (nth 1 entry))
       (setq value (cdr entry)))
     (if (stringp value)
         (fset (intern (concat "LaTeX-math-" value))
           (list 'lambda (list 'arg) (list 'interactive "*P")
             (list 'LaTeX-math-insert value
                   '(null (texmathp)))))))))))

This redefines all LaTeX-math-* functions defined inside LaTeX-math-initialize, in latex.el.