Emacs 中用户定义宏的默认颜色

Emacs 中用户定义宏的默认颜色

在编写 tex 文档时,我经常创建并包含一个包含许多用户定义的宏的文件,例如\newcommand{\pstar}{\ensuremath{\mathcal{P}^\star}}

我想设置 emacs 语法高亮,这样每次使用用户定义的宏时,例如,\pstar该宏都会以默认颜色高亮显示。我该如何做,而不必预先指定宏列表(如这个答案)?

答案1

可以按照您的要求做,但我不建议这样做。因此,我展示了 2 个解决方案:

不推荐的解决方案

我假设您没有对 init 文件进行字节编译。请将此函数放在 init 文件中的某个位置:

(defun LaTeX-fontify-parsed-macros ()
  "Add fontification support for parsed macros by AUCTeX."
  (let ((TeX-grop-string (string-to-char TeX-grop))
        macs macname args)
    ;; Process elements from `LaTeX-auto-optional' which look like
    ;; ("PSTAR" "1" "x")
    (dolist (mac LaTeX-auto-optional)
      (setq macname (car mac)
            args (string-to-number (cadr mac)))
      (push (list macname (concat LaTeX-optop
                                  (make-string (1- args)
                                               TeX-grop-string)))
            macs))
    ;; Process elements from `LaTeX-auto-arguments' which look like
    ;; ("PStar" "1")
    (dolist (mac LaTeX-auto-arguments)
      (setq macname (car mac)
            args (string-to-number (cadr mac)))
      (push (list macname (make-string args TeX-grop-string))
            macs))
    ;; Process only string elements from `TeX-auto-symbol' and ignore
    ;; lists inside the list
    (dolist (mac TeX-auto-symbol)
      (unless (listp mac)
        (push (list mac "") macs)))
    ;; Fontify the macros:
    (when (and macs
               (featurep 'font-latex)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords macs
                               'textual)) ))

现在在您的 .tex 文件中,将这一行添加到文件末尾的文件局部变量中:

%%% eval: (add-hook 'TeX-update-style-hook #'LaTeX-fontify-parsed-macros t)

它看起来应该是这样的:

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% eval: (add-hook 'TeX-update-style-hook #'LaTeX-fontify-parsed-macros t)
%%% End:

现在点击C-c C-n,您将获得适合您的宏的字体。

推荐的解决方案

将您的定义移到单独的包中,如下所示mymathmacros.sty

\ProvidesPackage{mymathmacros}
\newcommand{\pstar}{\ensuremath{\mathcal{P}^\star}}
\newcommand{\Pstar}{\ensuremath{\mathcal{P}^\star}}

\newcommand{\PStar}[1]{\ensuremath{\mathcal{P}^\star}}

\newcommand{\PSTar}[1][x]{\ensuremath{\mathcal{P}^\star}}
\newcommand{\PSTAR}[3][x]{\ensuremath{\mathcal{P}^\star}}
\endinput

将变量自定义TeX-style-private为您选择的目录,例如在 init 文件中添加如下一行:

(setq TeX-style-private (expand-file-name "~/.emacs.d/my-auctex-styles/"))

将这段代码保存在该目录中mymathmacros.el

(TeX-add-style-hook
 "mymathmacros"
 (lambda ()
   ;; Macros:
   (TeX-add-symbols
    '("pstar" 0)
    '("Pstar" 0)
    '("PStar"  1)
    '("PSTar" [ "argument" ] 1)
    '("PSTAR" [ "argument" ] 2))

   ;; Fontification:
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-lock-add-keywords '(("pstar" "")
                               ("Pstar" "")
                               ("PStar" "{")
                               ("PSTar" "[{")
                               ("PSTAR" "[{{"))
                             'textual)))
 LaTeX-dialect)

重新启动 Emacs,将该行添加\usepackage{mymathmacros}到您的 .tex 文件并点击C-c C-n(如果需要)。

相关内容