3

Is there a shortcut in AUCTEX/emacs that generates these escape parenthesis:

shorthand for "inline math environment"

\(  <cursor here>  \)

That puts the cursor in the middle of this "mini-environment thing"

so that I can write Math in the middle of a sentence.

I have tried the cheat sheets and the macros but I cannot get it to leave the cursor in the middle.

Alternatives/tips welcome (I am an undergrad trying to learn AUCTEX in my spare time)

3
  • 4
    You could use C-cC-e math which is the same thing but I just type \( then type the math then type \) that's only 4 keystrokes, it's hard to see how you would get a key combination to enter \( \) and then step past the \) at the end in less than 4 keystrokes. Actually I'd do $ math $ which is only two keystrokes. Jul 12, 2017 at 20:17
  • 2
    I've set up that (and similar things for braces and parentheses) in my init file; I explained this in an earlier answer, at https://tex.stackexchange.com/questions/27921/how-can-i-optimize-the-keyboard-for-frequent-latex-input/27923#27923 Jul 13, 2017 at 4:35
  • 2
    If you are using auctex, just put (setq LaTeX-electric-left-right-brace t) in your init file. When you are in a math environment typing \( results in what you want.
    – Name
    Jul 16, 2017 at 12:34

3 Answers 3

4

AUCTeX gives you have a couple of options out-of-the-box, without defining a function on your own. They're both described in the manual and aren't mutually exclusive, you can activate both and use them at the same time

First: TeX-electric-math

(add-hook 'plain-TeX-mode-hook
      (lambda () (set (make-local-variable 'TeX-electric-math)
              (cons "$" "$"))))
(add-hook 'LaTeX-mode-hook
      (lambda () (set (make-local-variable 'TeX-electric-math)
              (cons "\\(" "\\)"))))

With this code, you can just press $ and get \(...\) in LaTeX-mode

Second: LaTeX-electric-left-right-brace

(setq LaTeX-electric-left-right-brace t)

With this, you can type \( and the closing \) will be added after point.

2

For all my pairing needs I use the great package smartparenthesis. To include the pairs you require, do as follows:

First, install (using Melpa or whatever) smartparenthesis and then load it. I use usepackage for that, but the way suggested by the package's author is to simply run this:

(require 'smartparens-config)

The important code comes now:

(sp-with-modes '(tex-mode plain-tex-mode latex-mode)
  (sp-local-pair "\\\(" "\\\)"))

Note that you first escape the initial \, thus: \\, and then the opening parenthesis, thus: \(, which results in the complete sequence: \\\(. Ditto with the closing ones.

You can add all your beloved LaTeX pairs. This is my own full config (partially copied from someone else's):

(sp-with-modes '(tex-mode
                 plain-tex-mode
                 latex-mode
                 )
  ;; math modes, yay. The :actions are provided automatically if
  ; these pairs do not have global definition.
  (sp-local-pair "$" "$")
  (sp-local-pair "\[" "\]")
  (sp-local-pair "\{" "\}")
  (sp-local-pair "‘" "’")
  (sp-local-pair "“" "”")
  (sp-local-pair "\\begin" "\\end")
  ;;; tex-mode latex-mode
  (sp-local-tag "i" "\"<" "\">")
  (sp-local-pair "\\[" nil :unless '(sp-point-before-word-p))
  (sp-local-pair "$" nil :unless '(sp-point-before-word-p))
  )
0

Using @Phil Hirschhorn 's excellent examples I made a function which inserts "escape parentheses" when you press Alt-m

add it to your .emacs

M-m to activate

(defun escape-parentheses ()

  "we insert escape parenthesis for math-mode
   and move the cursor to the center"

    (interactive)
    (progn
        (insert "\\(   \\)")
        (backward-char 4)
    )
)


(global-set-key "\em"     'escape-parentheses)

You must log in to answer this question.

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