AUCTeX 切換數學模式

AUCTeX 切換數學模式

我用於$ ... $內聯數學和\[ ... \]顯示數學。我希望能夠透過選擇文字並呼叫 來在兩者之間切換TeX-insert-dollar

然而,據我了解,這個函數只允許循環

$ ... $ <-> $$ ... $$ <-> ...

\( ... \) <-> \[ ... \] <-> ...

(此行為是透過設定變數來控制的TeX-electric-math)。

有什麼方法可以根據我的需求自訂此行為(將其擴展到其他方程式環境也很有用,例如equationaligngather等)?

答案1

我終於設法像這樣做到了

(defun begin-end-regexps (env)
  (list env
        (replace-regexp-in-string "{\\([a-zA-Z]+\\)\\*}" "{\\1\\\\*}" (concat "\\\\begin{" env "}\\([^\0]*?\\)\\\\end{" env "}"))
        (concat "\\\\begin{" env "}\\1\\\\end{" env "}")))

(defun loop-math-modes (step)
  (if (texmathp)
      (let
          ((math-modes (list '("$" "\\$\\([^$\0]+?\\)\\$" "$\\1$")
                             '("\\[" "\\\\\\[\\([^\0]*?\\)\\\\\\]" "\\\\[\\1\\\\]")
                             (begin-end-regexps "equation")
                             (begin-end-regexps "equation*")
                             (begin-end-regexps "align")
                             (begin-end-regexps "align*")
                             (begin-end-regexps "gather")
                             (begin-end-regexps "gather*")))
           commands)

        (dolist (element math-modes) ;; get list of cars
          (setq commands
                (append commands (list (car element)))))
        (let
            ((math-mode-index (cl-position (car texmathp-why) commands :test 'equal))
             (current-pos (point)))
            (goto-char (cdr texmathp-why)) ;; for some reason save-excursion is not working
            (re-search-forward (nth 1 (nth math-mode-index math-modes)))
            (replace-match (nth 2 (nth (mod (+ math-mode-index step) (length math-modes)) math-modes)))
            (goto-char current-pos)))
    (message "Not inside math environment")))

(看這裡

相關內容