Emacs 和 latex 数学分隔符

Emacs 和 latex 数学分隔符

EMACS 中的命令forward-sexp可用于查找$闭合内联方程的:如果点位于$打开方程的之前,则将forward-sexp点移动到闭合的之后$。这对于嵌套方程执行正确的操作:例如,如果点位于之前$a + \text{b $c$}$,则将forward-sexp点移动到整个方程之后。有没有办法用auctex找到闭合和的\)或,也许用或?这在 EMACS 中似乎有点棘手,因为没有办法在语法表中存储多字符分隔符。\]\(\[texmathpfont-latex-match-math-env

答案1

我整理了一个可以满足您的需求的函数。它会搜索下一个 LaTeX 数学分隔符 – \(\)或– \[\]同时忽略注释,如果是开始分隔符,它会尝试找到匹配的分隔符。

(defun forward-latex-math ()
  "Move forward across the next LaTeX equation. It is meant work like `forward-sexp' but for LaTeX math delimiters."
  (interactive)
  (let ((count 1))
    ;; Search for either of the following \( \) \[ \]
    (re-search-forward-ignore-TeX-comments "\\\\(\\|\\\\)\\|\\\\\\[\\|\\\\]")
    (cond
     ;; If the search hits \(
     ((looking-back "\\\\(" (- (point) 2))
      (while (< 0 count)
        ;; Search for delimiters inside the equation
        (re-search-forward-ignore-TeX-comments "\\\\(\\|\\\\)")
        (if (looking-back "\\\\(" (- (point) 2))
            (setq count (1+ count))     ; If start of a nested level
          (setq count (1- count))))     ; If end of a nested level
      ;; Find the matching \)
      (re-search-forward "\\\\)" (eobp) t count))
     ;; If the search hits \[
     ((looking-back "\\\\\\[" (- (point) 2))
      (while (< 0 count)
        ;; Search for delimiters inside the equation
        (re-search-forward-ignore-TeX-comments "\\\\\\[\\|\\\\]")
        (if (looking-back "\\\\\\[" (- (point) 2))
            (setq count (1+ count))     ; If start of a nested level
          (setq count (1- count))))     ; If end of a nested level
      ;; Find the matching \]
      (re-search-forward "\\\\]" (eobp) t count)))))

(defun re-search-forward-ignore-TeX-comments (regexp)
  "Search for REGEXP and ignore TeX comments. Used by `forward-latex-math'."
  (re-search-forward regexp (eobp) t)
  ;; If in comment search to after it
  (while (TeX-in-comment)
    (forward-line)
    (re-search-forward regexp (eobp) t)))

要使用它,请将其放入 .emacs 中并通过 运行它M-xforward-latex-math。如果您想经常使用它,您可能需要将其绑定到键。

由于我刚开始学习 Lisp,我确信此代码可以在很多方面改进。如果您有任何建议,请发表评论。

相关内容