将 emacs latex 中所有类型的括号转换为 \left \right 形式

Convert all type of parentheses in emacs latex to \left \right form

在我的 .emacs 中我有以下功能

;; Put the point immediately after a closing paren. replace-matching-parens will
;; replace the closing ) with \right) and the matching start paren ( with
;; \left(.
(defun replace-matching-parens ()
  (interactive)
  (save-excursion
  (let ((end-point (point)))
    (backward-list)
    (let ((start-point (point)))
      (goto-char end-point)
      (re-search-backward ")" nil t)
      (replace-match " \\right)" nil nil)
      (goto-char start-point)
      (re-search-forward "(" nil t)
      (replace-match "\\left( " nil nil)))))

绑定到一个键以用 \left( 和 \right) 替换 ( 和 ) 的匹配出现。我怎样才能将其扩展到也适用于 [ ] 和 { } 对,以便它们分别被 \left[ 和 \right] 替换。 \left{ 和 \right}.

这应该可以帮助您入门。本质上,要使用 backward-list,您需要暂时为其他字符 (][}{) 提供左括号和右括号的语法。然后您需要调整搜索和替换以使用这些字符,而不仅仅是常规括号。

(eval-when-compile (require 'cl))   ; case

(defun replace-matching-parens (char)
  (interactive (list (char-before)))
  (unless (memq char '(?\) ?\] ?\}))
    (error "Cursor is not after `)', `]', or `}'"))
  (save-excursion
    (let ((syntable   (copy-syntax-table (syntax-table)))
          (end-point  (point)))
      (modify-syntax-entry ?\[ "(" syntable)
      (modify-syntax-entry ?\] ")" syntable)
      (modify-syntax-entry ?\{ "(" syntable)
      (modify-syntax-entry ?\} ")" syntable)
      (with-syntax-table syntable
        (backward-list)
        (let ((start-point  (point)))
          (goto-char end-point)
          (search-backward (format "%c" char) nil t)
          (replace-match (format " \\right%c" char) nil nil)
          (goto-char start-point)
          (search-forward (format "%c" (setq newchar  (case char
                                                        (?\) ?\( )
                                                        (?\] ?\[ )
                                                        (?\} ?\{ ))))
                          nil t)
          (replace-match (format "\\left%c " newchar) nil nil))))))