11

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$@?

0

1 Answer 1

16

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.

4
  • 1
    This... this is cool. I can survive without it, but is there any way to add this prefix argument automatically depending on whether or not we are in math mode? Dec 6, 2013 at 6:12
  • 2
    @SeanAllred see the updated answer :-).
    – giordano
    Dec 6, 2013 at 13:51
  • This should be the default behaviour. Can you do the same with LaTeX-math-cal? It has the same problem. Ideally, I would like to press ` c (letter) and have \mathcal{letter} written with or without dollar dollar signs depending on whether or not we are in math mode.
    – Zero
    Jun 21, 2021 at 0:38
  • I guess I'll just recomment to call your attention, I tried fixing this by myself but I couldn't do it :(
    – Zero
    Mar 21, 2023 at 1:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .