![如何在 emacs 中突出显示某些环境](https://linux22.com/image/242851/%E5%A6%82%E4%BD%95%E5%9C%A8%20emacs%20%E4%B8%AD%E7%AA%81%E5%87%BA%E6%98%BE%E7%A4%BA%E6%9F%90%E4%BA%9B%E7%8E%AF%E5%A2%83.png)
例如,我想突出显示theorem
和lemma
环境,但保持proof
环境不变。我尝试在字体锁定中搜索变量,但没有成功。
答案1
这并不容易实现,因为 AucTeX 在设计时并没有考虑到这种可扩展性。您有几个选择:
您可以自定义
font-latex-math-environments
添加theorem
和添加lemma
到列表中,然后 AucTeX 会将它们突出显示为数学环境。如果您不想将它们突出显示为数学,您可以复制/粘贴/修改负责
font-latex.el
识别数学环境的函数,并根据新情况进行调整。相关函数是和font-latex-extend-region-backwards-math-envII
和font-latex-match-math-envII
。前者需要添加到列表font-latex-extend-region-functions
,以便如果您在多行区域中间编辑文本,emacs 可以正确修复字体锁定。后者被添加到,以便font-latex-keywords-2
emacs 知道如何为您的新区域添加字体。以下代码将执行此操作:
(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 钩子中,以便每次都突出显示。