如何在 emacs 中突出显示某些环境

如何在 emacs 中突出显示某些环境

例如,我想突出显示theoremlemma环境,但保持proof环境不变。我尝试在字体锁定中搜索变量,但没有成功。

答案1

这并不容易实现,因为 AucTeX 在设计时并没有考虑到这种可扩展性。您有几个选择:

  • 您可以自定义font-latex-math-environments添加theorem和添加lemma到列表中,然后 AucTeX 会将它们突出显示为数学环境。

  • 如果您不想将它们突出显示为数学,您可以复制/粘贴/修改负责font-latex.el识别数学环境的函数,并根据新情况进行调整。相关函数是和font-latex-extend-region-backwards-math-envIIfont-latex-match-math-envII。前者需要添加到列表font-latex-extend-region-functions,以便如果您在多行区域中间编辑文本,emacs 可以正确修复字体锁定。后者被添加到,以便font-latex-keywords-2emacs 知道如何为您的新区域添加字体。以下代码将执行此操作:

(defcustom font-latex-lemma-environments
  '("theorem" "lemma")
  "List of lemma-like environment names for font locking."
  :type '(repeat string)
  :group 'font-latex)

(add-hook 'LaTeX-mode-hook
          (lambda ()
(defun font-latex-match-lemma-envII (limit)
  "Match lemma patterns up to LIMIT.
Used for patterns like:
\\begin{equation}
 fontified stuff
\\end{equation}
The \\begin{equation} and \\end{equation} are not fontified here."
  (when (re-search-forward (concat "\\\\begin[ \t]*{"
                   (regexp-opt font-latex-lemma-environments t)
                   "\\*?}")
               limit t)
    (let ((beg (match-end 0)) end)
      (if (re-search-forward (concat "\\\\end[ \t]*{"
                     (regexp-quote
                      (buffer-substring-no-properties
                       (match-beginning 1)
                       (match-end 0))))
                 ;; XXX: Should this rather be done by
                 ;; extending the region to be fontified?
                 (+ limit font-latex-multiline-boundary) 'move)
          (setq end (match-beginning 0))
    (goto-char beg)
        (setq end beg))
      (font-latex-put-multiline-property-maybe beg end)
      (store-match-data (list beg end))
      t)))

(defun font-latex-extend-region-backwards-lemma-envII (beg end)
  "Return position to extend region backwards for lemma environments.
Return nil if region does not have to be extended for a multiline
environment to fit in.  The region between the positions BEG and
END marks boundaries for searching for environment ends."
  (save-excursion
    (goto-char end)
    (catch 'extend
      (while (re-search-backward
          (concat "\\\\end[ \t]*{"
              (regexp-opt font-latex-lemma-environments t)
              "\\*?}") beg t)
    (when (and (re-search-backward (concat  "\\\\begin[ \t]*{"
                        (buffer-substring-no-properties
                         (match-beginning 1)
                         (match-end 0)))
                       (- beg font-latex-multiline-boundary) t)
           (< (point) beg))
      (throw 'extend (point))))
      nil)))

(add-to-list 'font-latex-keywords-2 '(font-latex-match-lemma-envII (0 'font-latex-warning-face t t)))
(add-to-list 'font-latex-extend-region-functions 'font-latex-extend-region-backwards-lemma-envII)
))

答案2

我不太清楚您说的突出显示是什么意思。您想更改开始/结束的字体还是整个环境的总体外观?

也许使用嗨锁模式你就能轻松实现一些事情。

如果这是您要找的,请告诉我,我们会想出一个简单的方法将其添加到 AucTex 钩子中,以便每次都突出显示。

相关内容