2

The tcolorbox package uses a different label syntax from the usual \label{name} one: it uses label={name}. Can I tell AUCTeX/RefTeX to enter labels for the tcolorbox environments with this new syntax? I also would like to know if I can make some other useful functions of RefTeX work with this syntax. I use the reftex-renumber-simple-labels quite a lot.

Here is a file exhibiting both syntaxes, and I have two theorem environments with the label-numbering in reverse order. When I run M-x reftex-renumber-simple-labels the two equation labels get changed, the labels in the theorems do not, and, in fact, the arguments of the two \refcommands also get (incorrectly) changed.

\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 output

2

AUCTeX provides a function called LaTeX-env-label-as-keyval which does the job: It inserts a key-value label inside an optional argument of an environment. In case of theorems library from tcolorbox, you have to put a little more effort in a style file in order to parse the environments defined with \newtcbtheorem. A prototype for an AUCTeX style can look like this:

;;; 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

In order to use this, customize the variable TeX-style-private to a directory of your choice or put something like this in your init file:

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

Save the code above as tcolorboxlib-theorems.el in that directory and restart Emacs. With a recent AUCTeX, it should load the style when you open your .tex file. It is necessary that you tell AUCTeX and RefTeX about the newly defined environment. I recommend to do this on a per-file basis with local-variables. Your test file will look like this then:

\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:

Open this file and hit C-c C-n if necessary. Then theorem should be available with C-c C-e theorem RET.

Reg. reftex-renumber-simple-labels, it cannot handle this syntax. I didn't have a chance to check why. But indeed, it messes up your document with the current implementation.

  • Thank you very much. I hope you will have time sometimes to look into modifying reftex-renumber-simple-labels. – Máté Wierdl Mar 28 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.