13

As pointed out in this answer, using \( and \) in order to enter inline math mode in LaTeX is the recommended practice. However, this takes a bit longer if one compares it to $.

Is there a way to make it quicker when using emacs+auctex? What are you guys doing in order to type it in quicker?

Dror
  • 22,613

4 Answers4

6

I use yasnippet. awesome package for inserting snippets. there is latex snippet bundle you can use. Infact there is already a written snippet to insert exactly what you are asking for.

N.N.
  • 36,163
kindahero
  • 236
  • It looks like the one thing I was after. Does the LaTeX bundle include a snippet for \( \) or did you add one manually? I found only the one for \[ and \]... – Dror Sep 05 '11 at 05:43
  • Yes, its already included. I type math and hit TAB. thats it. its dead easy to write a new snippet for your needs. – kindahero Sep 05 '11 at 06:29
  • Thnx! math+TAB generates a \[ \] pair - but I managed to create the snippet I needed! Thnx again. – Dror Sep 05 '11 at 07:18
  • @kindahero I would use a simpler key, as in shorter, for inline math because it may be used very often. – N.N. Dec 30 '11 at 13:59
  • @N.N. ofcourse yasnippet allows you to use what ever key you want. for eg: here in this case adding a line in your snippet file makes it happen # key: m – kindahero Dec 30 '11 at 15:29
  • 1
    @kindahero Indeed. I just meant that you might use inline math several times per paragraph and then you will be more effective if you use a short key. – N.N. Dec 30 '11 at 15:30
  • I tried to write a snippet and then saved it. Worked fine. But when I quit out of emacs and come back again that snippet is gone and it no longer works. –  Jul 15 '17 at 19:02
5

As described in its documentation, AUCTeX provides the function TeX-insert-dollar which inserts the two strings of TeX-electric-math and adjusts point position to between those strings when "$" is typed.

The following elisp can be added to a hook on LaTeX-mode to insert "\(" and "\)" then place the point after the opening paren when an unmatched "$" (i.e., the "$" is not closing an existing math environment) is typed:

(add-hook 'LaTeX-mode-hook
          (lambda () (set (make-variable-buffer-local 'TeX-electric-math)
                  (cons "\\(" "\\)"))))
giordano
  • 8,486
lafrenierejm
  • 51
  • 1
  • 4
3

You can setup a key binding in your .emacs file. This definition is based on the one for TeX-insert-braces (C-c{) from tex.el.

(add-hook 'LaTeX-mode-hook
  '(lambda ()
    (define-key TeX-mode-map "\C-cm" 'TeX-insert-inline-math)
    (defun TeX-insert-inline-math (arg)
      "Like TeX-insert-brackes but for \(...\)" (interactive "P")
      (if (TeX-active-mark)
        (progn
          (if (< (point) (mark)) (exchange-point-and-mark))
          (insert "\\)")
          (save-excursion (goto-char (mark)) (insert "\\(")))
          (insert "\\(")
          (save-excursion
            (if arg (forward-sexp (prefix-numeric-value arg)))
            (insert "\\)"))))))
Audrey
  • 28,881
2

Pressing $ in AUCTeX runs TeX-insert-dollar, the behaviour of which is controlled by the variable TeX-electric-math which can be set to use any pair of symbols instead of the default pair of $.