AUCTeX 数式モードの切り替え

AUCTeX 数式モードの切り替え

$ ... $インライン数式には を、表示数式には を使用します\[ ... \]。テキストを選択して を呼び出すことで、この 2 つを切り替えることができるようにしたいと思いますTeX-insert-dollar

しかし、私が理解している限り、この機能はサイクルのみを許可します

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

そして

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

(この動作は変数 を設定することによって制御されますTeX-electric-math)。

equationこの動作を自分のニーズに合わせてカスタマイズする方法はありますか ( 、、などalignの他の方程式環境に拡張すると便利ですgather)?

答え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")))

(見るここ

関連情報