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

From: Arash Esbati <[email-masked]>
Date: Sun, 11 Sep 2022 22:40:17 +0200

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

> It now seems to work perfectly.  I tried it not only on the test file
> but also on the book I have been working on which loads quite a few
> .tex files.

Thanks for testing, also on a bigger project, that gives me a better feeling.

> Should I put this "defun" into my .emacs for the time being, till it
> gets incorporated into reftex?

I think I will install this version which seems a little more robust to
me.

--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)
        (opt-re (concat "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*}[^}{]*\\)*"
                        "}[^}{]*\\)*"
                        "}[^][]*\\)*"))
        (man-re (concat "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*"
                        "\\(?:{[^}{]*}[^}{]*\\)*"
                        "}[^}{]*\\)*"
                        "}[^}{*\\)*"))
        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:]]*"
                                          "\\[[^][]*"
                                          opt-re
                                          (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:]]*"
                                          "{[^}{]*"
                                          man-re
                                          (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---

And yes, you will have to put in your .emacs wrapped in

    (with-eval-after-load 'reftex-global
      (defun ...)
    )

until Emacs 29 is out and you're using it (unless you want to built the
version out of Emacs repo and use it)

Best, Arash