AUCTeX alterna modos matemáticos

AUCTeX alterna modos matemáticos

Eu uso $ ... $para matemática embutida e \[ ... \]para exibição de matemática. Gostaria de poder alternar entre os dois selecionando o texto e chamando TeX-insert-dollar.

Porém, pelo que entendi, esta função só permite os ciclos

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

e

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

(esse comportamento é controlado definindo a variável TeX-electric-math).

Existe alguma maneira de personalizar esse comportamento de acordo com minhas necessidades (também seria útil estendê-lo para outros ambientes de equações, como equation, align, gather, etc.)?

Responder1

Eu finalmente consegui fazer isso assim

(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")))

(veraqui)

informação relacionada