以与 Emacs+AUCTeX 中的 \label 相同的方式突出显示 \nllabel

以与 Emacs+AUCTeX 中的 \label 相同的方式突出显示 \nllabel

我正在使用 Emacs 和 AUCTeX 编辑使用 的 LaTeX 代码algorithm2e。如何让 Emacs 以\nllabel{stuff}与 相同的方式突出显示\label{stuff}

答案1

您可以为 AUCTeX 编写一个支持文件(样式),以便让您的打字生活更轻松。我不使用algorithm2e,但这种样式的骨架可能看起来像这样:

;;; algorithm2e.el --- AUCTeX style for `algorithm2e.sty' (v5.1)

(defun LaTeX-algorithm2e-lineend ()
  "Insert \"\\;\" to terminate lines in env's provided by algorithm2e package."
  (end-of-line 0)
  (delete-horizontal-space)
  (insert "\\;")
  (forward-line 1)
  (indent-according-to-mode))

(TeX-add-style-hook
 "algorithm2e"
 (lambda ()

   (let ((envs '("algorithm" "algorithm*"
         "procedure" "procedure*"
         "function"  "function*")))
     (dolist (env envs)
       ;; Add env's to `LaTeX-item-list' in order to run
       ;; `LaTeX-algorithm2e-lineend' when `M-RET' is invoked
       (add-to-list 'LaTeX-item-list `(,env . LaTeX-algorithm2e-lineend) t)

       ;; Tell AUCTeX that we want to prefix the labels with `alg:'
       (add-to-list 'LaTeX-label-alist `(,env . "alg:") t)

       ;; Tell RefTeX the same thing:
       (when (fboundp 'reftex-add-label-environments)
         (reftex-add-label-environments
          `((,env ?a "alg:" "~\\ref{%s}" caption (regexp "[Al]lgorithm")))))))

   (LaTeX-add-environments
    '("algorithm")
    '("algorithm*")
    '("procedure")
    '("procedure*")
    '("function")
    '("function*"))

   (TeX-add-symbols
    '("KwData" "Keyword Data")
    '("KwResult" "Keyword Result")
    '("nllabel" TeX-arg-define-label))

   ;; Fontification
   (when (and (featurep 'font-latex)
          (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("nllabel" "{"))
                  'reference)))
 LaTeX-dialect)

(defvar LaTeX-algorithm2e-package-options
  '(;; 7.1 Language option
    "english" "french" "frenchkw" "german" "germankw"
    "ngerman" "spanish" "spanishkw" "italiano" "italianokw"
    "portuguese" "portuguesekw" "croatian" "croatiankw"
    "czech" "czechkw" "turkish" "turkishkw" "onelanguage"
    ;; 7.2 Compatibility issue
    "algo2e" "endfloat" "norelsize" "slide" "nokwfunc"
    ;; 7.3.1 boxed, ruled, plain environment
    "boxed" "boxruled" "ruled" "algoruled" "tworuled" "plain"
    ;; 7.3.2 algorithm numbering
    "algochapter" "algosection" "algopart" "procnumbered"
    ;; 7.3.3 figure and toc
    ;; ...
    )
  "Package options for the algorithm2e package.")

;;; algorithm2e.el ends here

作为一项功能,您可以M-RET在定义的环境中点击行尾,然后 Emacs 会插入\;一个换行符。

为了使用这种样式,请将变量设置TeX-style-private为您选择的目录并将代码保存在algorithm2e.el那里。下次加载 .tex 文件时,AUCTeX 应该会加载样式(记得(setq TeX-parse-self t)在您的初始化文件中设置)。

您可以扩展此文件,一旦您拥有宏,您就可以将此文件提交给 AUCTeX devel-list 并将其与 AUCTeX 捆绑在一起,并使其他用户可以使用。


通过描述的设置,测试文件对我来说如下所示:

在此处输入图片描述

相关内容