Subject: bug#57720: 13.1.4; renumbering simple labels doesn't work with tcolorbox

From: Arash Esbati <[email-masked]>
Date: Sun, 11 Sep 2022 12:13:06 +0200

Wierdl Máté <[email-masked]> writes:

> I do not know if this is a reftex or AUCTeX bug.

RefTeX.

> When using theoremlike environments from the tcolorbox, the function
> reftex-renumber-simple-labels messes up. It renumbers the labels in
> references but does nothing to the labels themselves.

The issue is more in the function `reftex-translate' which is called
inside `reftex-renumber-simple-labels': The former is not prepared to
deal with labels inside arguments of environments.  Can you try this
change by copying to scratch buffer, opening your .tex file, eval'ing
this function and then running `reftex-renumber-simple-labels' and see
if it works?

--8<---------------cut here---------------start------------->8---
(defun reftex-translate (files search-re translate-alist error-fmt test)
  ;; In FILES, look for SEARCH-RE and replace match 1 of it with
  ;; its association in TRANSLATE-ALIST.
  ;; If we do not find an association and TEST is non-nil, query
  ;; to ignore the problematic string.
  ;; If TEST is nil, it is ignored without query.
  ;; Return the number of replacements.
  (let ((n 0) file label match-data buf macro pos cell)
    (while (setq file (pop files))
      (setq buf (reftex-get-file-buffer-force file))
      (unless buf
        (error "No such file %s" file))
      (set-buffer buf)
      (save-excursion
        (save-restriction
          (widen)
          (goto-char (point-min))
          (while (re-search-forward search-re nil t)
            (backward-char)
            (save-excursion
              (setq label (reftex-match-string 1)
                    cell (assoc label translate-alist)
                    match-data (match-data)
                    macro (reftex-what-macro 1)
                    pos (cdr macro))
              (goto-char (or pos (point)))
              (when (and macro
                         (or (looking-at "\\\\ref")
                             (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]")
                             (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]")
                             (looking-at (format
                                          reftex-find-label-regexp-format
                                          (regexp-quote label)))
                             ;; In case the label-keyval is inside an
                             ;; optional argument to \begin{env}
                             (looking-at (concat
                                          "\\\\begin[[:space:]]*{[^}]+}"
                                          "[[:space:]]*"
                                          "\\[[^][]*"
                                          (format
                                           reftex-find-label-regexp-format
                                           (regexp-quote label))
                                          "[^][]*\\]"))
                             ;; In case the label-keyval is inside the
                             ;; first mandatory argument to \begin{env}
                             (looking-at (concat
                                          "\\\\begin[[:space:]]*{[^}]+}"
                                          "[[:space:]]*"
                                          "{[^}{]*"
                                          (format
                                           reftex-find-label-regexp-format
                                           (regexp-quote label))
                                          "[^}{]*}"))))
                ;; OK, we should replace it.
                (set-match-data match-data)
                (cond
                 ((and test (not cell))
                  ;; We've got a problem
                  (unwind-protect
                      (progn
                        (reftex-highlight 1 (match-beginning 0) (match-end 0))
                        (ding)
                        (or (y-or-n-p (format error-fmt label))
                            (error "Abort")))
                    (reftex-unhighlight 1)))
                 ((and test cell)
                  (cl-incf n))
                 ((and (not test) cell)
                  ;; Replace
                  (goto-char (match-beginning 1))
                  (delete-region (match-beginning 1) (match-end 1))
                  (insert (cdr cell)))
                 (t nil))))))))
    n))
--8<---------------cut here---------------end--------------->8---

TIA.  Best, Arash