AUCTeX/RefTeX:如何处理使用“label={name}”语法而不是“\label{name}”的标签?

AUCTeX/RefTeX:如何处理使用“label={name}”语法而不是“\label{name}”的标签?

tcolorbox包使用与通常不同的标签语法\label{name}:它使用label={name}。我可以AUCTeX/RefTeX用这种新语法输入环境标签tcolorbox吗?我还想知道我是否可以使用此语法使其他一些有用的功能工作。我经常RefTeX使用。reftex-renumber-simple-labels

这是一个展示这两种语法的文件,我有两个定理环境,标签编号顺序相反。当我运行M-x reftex-renumber-simple-labels这两个方程标签时,定理中的标签没有改变,事实上,这两个\ref命令的参数也(错误地)改变了。

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem{theorem}{Theorem}{}{}
\begin{document}
\begin{theorem}[label={thm:2}]{Squares series}{}
  $\sum 1/n^2<\infty$. 
\end{theorem}
In \ref{thm:2}.
\end{document}
\begin{equation}
  \label{eq:2}
  \sum 1/n^2<\infty.
\end{equation}

\begin{theorem}[label={thm:1}]{Harmonic series}{}
  $\sum 1/n=\infty$. 
\end{theorem}
In \ref{thm:1}.
\begin{equation}
  \label{eq:1}
  \sum 1/n=\infty.
\end{equation}
\end{document}

pdflate 输出

答案1

AUCTeX 提供了一个名为 的函数LaTeX-env-label-as-keyval来完成这项工作:它在环境的可选参数中插入一个键值标签。对于theorems来自 的库tcolorbox,您必须在样式文件中投入更多精力才能解析用 定义的环境\newtcbtheorem。AUCTeX 样式的原型如下所示:

;;; Code:

; Needed for auto-parsing:
(require 'tex)

(defvar LaTeX-tcolorbox-lib-theorems-keyval-options
  '(;; 14.3 Option Keys of the Library
    ("separator sign")
    ("separator sign colon")
    ("separator sign dash")
    ("separator sign none")
    ("description delimiters" ("{}{}"))
    ("description delimiters parenthesis")
    ("description delimiters none")
    ("description font"
     ("\\rmfamily" "\\sffamily" "\\ttfamily" "\\mdseries" "\\bfseries"
      "\\upshape" "\\itshape" "\\slshape" "\\scshape"
      "\\tiny"  "\\scriptsize" "\\footnotesize"
      "\\small" "\\normalsize" "\\large"
      "\\Large" "\\LARGE" "\\huge" "\\Huge" "\\normalfont"))
    ("description formatter")
    ("terminator sign")
    ("terminator sign colon")
    ("terminator sign dash")
    ("terminator sign none")
    ("label separator")
    ("theorem full label supplement")
    ("theorem label supplement")
    ("theorem name and number")
    ("theorem number and name")
    ("theorem name")
    ("theorem" ("{}{}{}{}"))
    ("highlight math")
    ("highlight math style" ("{}"))
    ("math upper")
    ("math lower")
    ("math")
    ("ams equation upper")
    ("ams equation lower")
    ("ams equation")
    ;; ...
    )
  "Key=value options for theorems library from tcolorbox.")

;; Setup for \newtcbtheorem:
(TeX-auto-add-type "tcolorbox-lib-theorems-newtcbtheorem" "LaTeX")

(defvar LaTeX-tcolorbox-lib-theorems-newtcbtheorem-regexp
  `(,(concat "\\\\newtcbtheorem"
         "[ \t\n\r%]*"
         "\\(?:\\[[^][]*"
           "\\(?:{[^}{]*"
             "\\(?:{[^}{]*"
               "\\(?:{[^}{]*}[^}{]*\\)*"
             "}[^}{]*\\)*"
           "}[^][]*\\)*"
         "\\]\\)?"
         "[ \t\n\r%]*"
         "{\\([a-zA-Z0-9]+\\)}")
    1 LaTeX-auto-tcolorbox-lib-theorems-newtcbtheorem)
  "Matches the name argument of \\newtcbtheorem macro.")

(defun LaTeX-tcolorbox-lib-theorems-auto-prepare ()
  "Reset `LaTeX-auto-tcolorbox-lib-theorems-newtcbtheorem' before parsing."
  (setq LaTeX-auto-tcolorbox-lib-theorems-newtcbtheorem nil))

(defun LaTeX-tcolorbox-lib-theorems-auto-cleanup ()
  "Process user defined theorems with \\newtcbtheorem.
Add every environment name to local version of
`font-latex-math-environments' and `texmathp-tex-commands' and
run the function `texmathp-compile' if necessary."
  ;; Use a local versions so we don't interfere with user customizations
  (when (boundp 'font-latex-math-environments)
    (make-local-variable 'font-latex-math-environments))
  (when (boundp 'texmathp-tex-commands)
    (make-local-variable 'texmathp-tex-commands))
  (let (texmathp-flag)
    (dolist (env (mapcar #'car (LaTeX-tcolorbox-lib-theorems-newtcbtheorem-list)))
      (LaTeX-add-environments `(,env LaTeX-tcolorbox-lib-theorems-env))
      (when (boundp 'font-latex-math-environments)
    (add-to-list 'font-latex-math-environments env t))
      (when (and (boundp 'texmathp-tex-commands)
         (not (assoc env texmathp-tex-commands)))
    (add-to-list 'texmathp-tex-commands `(,env env-on) t)
    (setq texmathp-flag t)))
    (when texmathp-flag (texmathp-compile))))

(add-hook 'TeX-auto-prepare-hook
      #'LaTeX-tcolorbox-lib-theorems-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook
      #'LaTeX-tcolorbox-lib-theorems-auto-cleanup t)
(add-hook 'TeX-update-style-hook
      #'TeX-auto-parse t)

(defun LaTeX-tcolorbox-lib-theorems-env (environment)
  "Insert theorems ENVIRONMENT, ask for arguments and insert a label."
  (LaTeX-insert-environment
   environment
   (let ((opts (TeX-read-key-val
        t (append
           LaTeX-tcolorbox-lib-theorems-keyval-options
           `(("description color"
              ,(mapcar #'car (LaTeX-xcolor-definecolor-list))))
           LaTeX-tcolorbox-keyval-options-local)))
     (title (TeX-read-string
         (TeX-argument-prompt nil nil "Title"))))
     (concat
      (when (and opts (not (string= opts "")))
    (format "[%s]" opts))
      (concat TeX-grop title TeX-grcl)
      TeX-grop TeX-grcl)))
  (LaTeX-env-label-as-keyval nil nil nil environment))

(TeX-add-style-hook
 "tcolorboxlib-theorems"
 (lambda ()

   ;; Add the style to the parser
   (TeX-auto-add-regexp LaTeX-tcolorbox-lib-theorems-newtcbtheorem-regexp)

   ;; Append key-vals from library to `LaTeX-tcolorbox-keyval-options-full':
   (setq LaTeX-tcolorbox-keyval-options-full
     (append LaTeX-tcolorbox-lib-theorems-keyval-options
         `(("description color"
            ,(mapcar #'car (LaTeX-xcolor-definecolor-list))))
         LaTeX-tcolorbox-keyval-options-full))

   ;; This library loads amsmath:
   (TeX-run-style-hooks "amsmath")

   ;; Add fix macros to `texmathp-tex-commands':
   (when (boundp 'texmathp-tex-commands)
     (make-local-variable 'texmathp-tex-commands)
     (add-to-list 'texmathp-tex-commands '("\\tcboxmath"   arg-on) t)
     (add-to-list 'texmathp-tex-commands '("\\tcbhighmath" arg-on) t))

   (TeX-add-symbols
    ;; 16.1 Macros of the Library
    `("newtcbtheorem"
      [ TeX-arg-key-val LaTeX-tcolorbox-init-options ]
      (TeX-arg-eval
       (lambda ()
     (let ((name (TeX-read-string
              (TeX-argument-prompt optional nil "Name"))))
       (LaTeX-add-tcolorbox-lib-theorems-newtcbtheorems name)
       (LaTeX-tcolorbox-lib-theorems-auto-cleanup)
       (format "%s" name))))
      "Display name"
      (TeX-arg-key-val ,(append
             LaTeX-tcolorbox-lib-theorems-keyval-options
             `(("description color"
                ,(mapcar #'car (LaTeX-xcolor-definecolor-list))))
             LaTeX-tcolorbox-keyval-options-local))
      ;; Leave the prefix argument empty as AUCTeX and RefTeX both
      ;; cannot handle prefix-separator-marker combination:
      (TeX-arg-literal "{")
      (TeX-arg-literal "}"))

    `("renewtcbtheorem"
      [ TeX-arg-key-val LaTeX-tcolorbox-init-options ]
      (TeX-arg-eval completing-read
            (TeX-argument-prompt optional nil "Name")
            (LaTeX-tcolorbox-lib-theorems-newtcbtheorem-list))
      "Display name"
      (TeX-arg-key-val ,(append
             LaTeX-tcolorbox-lib-theorems-keyval-options
             LaTeX-tcolorbox-keyval-options-local))
      ;; Leave the prefix argument empty as AUCTeX and RefTeX both
      ;; cannot handle prefix-separator-marker combination:
      (TeX-arg-literal "{")
      (TeX-arg-literal "}"))

    '("tcboxmath"
      [ TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local ]
      t)

    '("tcbhighmath"
      [ TeX-arg-key-val LaTeX-tcolorbox-keyval-options-local ]
      t) )

   ;; Fontification
   (when (and (featurep 'font-latex)
          (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("newtcbtheorem"   "[{{{{")
                                ("renewtcbtheorem" "[{{{{"))
                              'function)
     (font-latex-add-keywords '(("tcboxmath"   "[{")
                                ("tcbhighmath" "[{"))
                              'math-command)))
 LaTeX-dialect)

;;; tcolorboxlib-theorems.el ends here

为了使用此功能,请将变量自定义TeX-style-private为您选择的目录或将如下内容放入您的初始化文件中:

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

将上面的代码保存tcolorboxlib-theorems.el在该目录中并重新启动 Emacs。使用最新的 AUCTeX,它应该会在您打开 .tex 文件时加载样式。您必须告诉 AUCTeX 和 RefTeX 有关新定义的环境。我建议使用局部变量在每个文件上执行此操作。您的测试文件将如下所示:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem{theorem}{Theorem}{}{}
\begin{document}
\begin{theorem}[label={thm:2}]{Squares series}{}
  $\sum 1/n^2<\infty$. 
\end{theorem}
In \ref{thm:2}.
\end{document}
\begin{equation}
  \label{eq:2}
  \sum 1/n^2<\infty.
\end{equation}

\begin{theorem}[label={thm:1}]{Harmonic series}{}
  $\sum 1/n=\infty$. 
\end{theorem}
In \ref{thm:1}.
\begin{equation}
  \label{eq:1}
  \sum 1/n=\infty.
\end{equation}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% eval: (add-to-list 'LaTeX-label-alist '("theorem" . "thm"))
%%% eval: (reftex-add-label-environments '(("theorem" ?m "thm:" "~\\ref{%s}" nil ("Theorem" "theorem") nil)))
%%% End:

打开此文件,C-c C-n如果需要,点击。然后theorem应该可以使用C-c C-e theorem RET

Reg. reftex-renumber-simple-labels,它无法处理此语法。我没有机会检查原因。但确实,它会用当前的实现弄乱您的文档。

相关内容