2

I have enabled (LaTeX-electric-left-right-brace) in AUCTeX. Thus, when I type \bigl(, the corresponding closing \bigr) is automatically inserted.

Suppose that I already have written (foo) and now decide that I want to use big parentheses.

How can I make AUCTeX insert the matching \bigr when I insert \bigl?

  1. I have (foo)
  2. I insert \bigl, thus I get \bigl(foo).
  3. AUCTeX shall now automatically produce \bigl(foo\bigr).

Of course it should insert the \bigr at the matching parentheses. If I have (foo(bar)), then \bigl(foo(bar)\bigr) should be produced.

  • 2
    I think it's probably more bother to get the behaviour you want than just to do this by hand the few times it comes up. Relevant xkcd. – Seamus Sep 19 '16 at 10:00
3

I ended up doing things a bit different.

I defined a kbd-macro and saved it to my init.el (see https://www.gnu.org/software/emacs/manual/html_node/emacs/Save-Keyboard-Macro.html). (Add this to your init.el)

(fset '\bigl1
   [?\\ ?b ?i ?g ?l ?\C-\M-n left ?\\ ?b ?i ?g ?r right ?\C-\M-p])

Then I bounded it to some keyboard command (add this to your init.el)

(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-1")  #'bigl1))

So pressing s-1 (windowskey (on most machines) together with 1) produces \bigl(foo\bigl) if we had (foo) (and cursor at the left parentheses).

To get this behaviour as well for Bigl and biggl I ended up with

(fset '\bigl1
      [?\\ ?b ?i ?g ?l ?\C-\M-n left ?\\ ?b ?i ?g ?r right ?\C-\M-p])
(fset '\bigl2
      [?\\ ?B ?i ?g ?l ?\C-\M-n left ?\\ ?B ?i ?g ?r right ?\C-\M-p])
(fset '\bigl3
      [?\\ ?b ?i ?g ?g ?l ?\C-\M-n left ?\\ ?b ?i ?g ?g ?r right ?\C-\M-p])
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-1")  #'bigl1))
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-2")  #'bigl2))
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-3")  #'bigl3))

in my init.el file. (first)

Then I programmed a bit around and now I can add (by pressing s-1, s-2, s-3, s-4) \bigl/\bigr, \Bigl/\Bigr, \biggl/\biggr, \Biggl/\Biggr to the next(right of the coursor) (,),[,],\{,\} and its counterpart to the matching ),(,],[,\},\{

(defun addthings-left-right-latex-parentheses (links rechts)
  "Adds first Input to the left and the second Input 
   to the right of the next  (,),[,],\{,\} (right of the coursor)
   to the matching ),(,],[,\},\{"
  (interactive "sWhat do you want left of parentheses?:
   \nsWhat do you want right of parentheses?: ")  
  (when (re-search-forward "[][()]\\|\\\\[{}]")
(if (or (char-equal (char-before) ?\() (char-equal (char-before) ?\[))
    (progn (backward-char)
       (insert links)
       (forward-list)
       (backward-char)
       (insert rechts))
  (if (or (char-equal (char-before) ?\)) (char-equal (char-before) ?\]))
      (progn
    (backward-list)
    (insert links)
    (forward-list)
    (backward-char)
    (insert rechts))
      (if (char-equal (char-before) ?\{)
      (progn
        (backward-char)(backward-char)
        (insert links)
        (forward-char)(forward-char)
        (setq counter 1)      
        (while  (< 0 counter)
          (progn
        (re-search-forward "\\\\[{}]")
        (if (char-equal (char-before) ?\{)
          (setq counter (+ counter 1))
          (setq counter (+ counter -1)))))
        (backward-char)(backward-char)
        (insert rechts))
    (progn
      (backward-char)(backward-char)
      (insert rechts)
      (setq counter 1)    
        (while  (< 0 counter)
          (progn
        (re-search-backward "\\\\[{}]")
        (forward-char)
        (if (char-equal (char-after) ?\})
          (setq counter (+ counter 1))
          (setq counter (+ counter -1)))))
        (backward-char)
        (insert links)))))))
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-1")
 (lambda () (interactive)
   (addthings-left-right-latex-parentheses "\\bigl" "\\bigr"))))
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-2")
 (lambda () (interactive)
   (addthings-left-right-latex-parentheses "\\Bigl" "\\Bigr"))))
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-3")
 (lambda () (interactive)
   (addthings-left-right-latex-parentheses "\\biggl" "\\biggr"))))
(eval-after-load 'latex 
  '(define-key LaTeX-mode-map (kbd "s-4")
 (lambda () (interactive)
   (addthings-left-right-latex-parentheses "\\Biggl" "\\Biggr"))))

So there are no more excuses for not making your (mathematical) latex-parentheses more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.