GNU bug report logs - #36111
Support cleveref labels in AUCTeX and RefTeX

Previous Next

Package: auctex;

Reported by: Ryan Kavanagh <rak <at> debian.org>

Date: Thu, 6 Jun 2019 02:33:02 UTC

Severity: wishlist

Done: Arash Esbati <arash <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 36111 in the body.
You can then email your comments to 36111 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-auctex <at> gnu.org:
bug#36111; Package auctex. (Thu, 06 Jun 2019 02:33:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ryan Kavanagh <rak <at> debian.org>:
New bug report received and forwarded. Copy sent to bug-auctex <at> gnu.org. (Thu, 06 Jun 2019 02:33:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ryan Kavanagh <rak <at> debian.org>
To: submit <at> debbugs.gnu.org
Subject: Support cleveref labels in AUCTeX and RefTeX
Date: Wed, 5 Jun 2019 20:29:08 -0400
[Message part 1 (text/plain, inline)]
Package: auctex
Severity: wishlist

I use the cleveref package, which allows annotating \label commands with
an optional argument. Unfortunately, this prevents both AUCTeX and
RefTeX from detecting those labels.

To illustrate, here is some LaTeX code from which AUCTeX and RefTeX fail
to extract the label "eq:23":

    \usepackage{cleveref}
    \crefname{diagram}{diagram}{diagrams}
    % ...
    \begin{equation}
      \label[diagram]{eq:23}
      1 = 1
    \end{equation}

This is problematic, because the function reftex-label will reuse the
label "eq:23".

I am not sufficiently familiar with emacs lisp to submit a patch, but I
think I have found some of the places that should be updated:

* The variable LaTeX-auto-label-regexp-list in latex.el

* The function reftex-parse-from-file hardcodes the prefix "\\label{".
  Perhaps this could be a customizable regexp?

More details can be found in this question:
https://tex.stackexchange.com/q/494337/6128 .

Please add support for cleveref labels to AUCTeX and RefTeX.

Best,
Ryan

-- 
|)|/  Ryan Kavanagh      | GPG: 4E46 9519 ED67 7734 268F
|\|\  https://rak.ac     |      BD95 8F7B F8FC 4A11 C97A
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-auctex <at> gnu.org:
bug#36111; Package auctex. (Thu, 06 Jun 2019 23:11:02 GMT) Full text and rfc822 format available.

Message #8 received at 36111 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: Ryan Kavanagh <rak <at> debian.org>
Cc: 36111 <at> debbugs.gnu.org
Subject: Re: bug#36111: Support cleveref labels in AUCTeX and RefTeX
Date: Fri, 07 Jun 2019 01:07:28 +0200
[Message part 1 (text/plain, inline)]
Ryan Kavanagh <rak <at> debian.org> writes:

> I use the cleveref package, which allows annotating \label commands with
> an optional argument. Unfortunately, this prevents both AUCTeX and
> RefTeX from detecting those labels.

Hi Ryan,

thanks for your report.  Yes, AUCTeX and RefTeX both get confused with
that optional argument.

> I am not sufficiently familiar with emacs lisp to submit a patch, but I
> think I have found some of the places that should be updated:
>
> * The variable LaTeX-auto-label-regexp-list in latex.el

latex.el provides support for vanilla LaTeX.  Additions by packages
should go into AUCTeX styles.

> * The function reftex-parse-from-file hardcodes the prefix "\\label{".
>   Perhaps this could be a customizable regexp?

RefTeX provides a customizable variable for this purpose called
`reftex-label-regexps'.

> Please add support for cleveref labels to AUCTeX and RefTeX.

This can be done within cleveref.el.  A modified version is attached.
[cleveref.el (application/emacs-lisp, attachment)]
[Message part 3 (text/plain, inline)]
Can you please customize the variable `TeX-style-private' to a directory
of your choice, save the file there, restart Emacs and open your test
file and report back if it works?  I will install the patch afterwards.

In case you're interested in the changes:

--8<---------------cut here---------------start------------->8---
diff --git a/style/cleveref.el b/style/cleveref.el
index 2774dcbd..c241a1cc 100644
--- a/style/cleveref.el
+++ b/style/cleveref.el
@@ -1,6 +1,6 @@
 ;;; cleveref.el --- Style hook for the `cleveref.sty' package.

-;; Copyright (C) 2014--2016, 2018 Free Software Foundation, Inc.
+;; Copyright (C) 2014--2019 Free Software Foundation, Inc.

 ;; Author: Matthew Leach <matthew <at> mattleach.net>
 ;; Maintainer: auctex-devel <at> gnu.org
@@ -47,22 +47,41 @@ string."
           (labels-string (mapconcat #'identity labels ",")))
       (TeX-argument-insert labels-string optional))))

+(defun TeX-arg-cleveref-crossref-type (optional &optional prompt)
+  "Insert the cross-reference type for macros of cleveref package.
+If OPTIONAL is non-nil, insert the resulting value in brackets.
+Use PROMPT as the prompt string."
+  (let* ((type (mapcar #'list
+                      '("appendix" "subappendix" "subsubappendix"
+                        "subsubsubappendix" "subfigure" "subtable"
+                        "subequation")))
+        (types (append type (LaTeX-counter-list))))
+    (TeX-argument-insert
+     (completing-read (TeX-argument-prompt optional prompt "Type") types)
+     optional)))
+
+(defvar LaTeX-cleveref-label-regexp
+  '("\\\\label\\[[^]]*\\]{\\([^\n\r%\\{}]+\\)}" 1 LaTeX-auto-label)
+  "Regexp matching a \\label incl. an optional argument.")
+
 (TeX-add-style-hook
  "cleveref"
  (lambda ()
+
    (TeX-add-symbols
+    ;; 4 Typesetting Cross-References
     '("cref" TeX-arg-cleveref-multiple-labels)
     '("Cref" TeX-arg-cleveref-multiple-labels)
     '("crefrange" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
     '("Crefrange" (TeX-arg-ref "key (first)") (TeX-arg-ref "Key (last)"))
-    '("cpageref" TeX-arg-cleveref-multiple-labels)
-    '("Cpageref" TeX-arg-cleveref-multiple-labels)
-    '("cpagerefrange" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
-    '("Cpagerefrange" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
     '("cref*" TeX-arg-cleveref-multiple-labels)
     '("Cref*" TeX-arg-cleveref-multiple-labels)
     '("crefrange*" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
     '("Crefrange*" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
+    '("cpageref" TeX-arg-cleveref-multiple-labels)
+    '("Cpageref" TeX-arg-cleveref-multiple-labels)
+    '("cpagerefrange" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
+    '("Cpagerefrange" (TeX-arg-ref "Key (first)") (TeX-arg-ref "Key (last)"))
     '("namecref" TeX-arg-ref)
     '("nameCref" TeX-arg-ref)
     '("lcnamecref" TeX-arg-ref)
@@ -70,7 +89,18 @@ string."
     '("nameCrefs" TeX-arg-ref)
     '("lcnamecrefs" TeX-arg-ref)
     '("labelcref" TeX-arg-cleveref-multiple-labels)
-    '("labelcpageref" TeX-arg-cleveref-multiple-labels))
+    '("labelcpageref" TeX-arg-cleveref-multiple-labels)
+    ;; 6 Overriding the Cross-Reference Type
+    '("crefalias" TeX-arg-counter "Type")
+    '("label" [ TeX-arg-cleveref-crossref-type ] TeX-arg-define-label)
+
+    ;; 8.1.2 Customising Individual Cross-Reference Types
+    '("crefname" TeX-arg-cleveref-crossref-type
+      "Singular name" "Plural name")
+    '("Crefname" TeX-arg-cleveref-crossref-type
+      "Singular name" "Plural name")
+    '("creflabelformat" TeX-arg-cleveref-crossref-type t)
+    '("crefrangelabelformat" TeX-arg-cleveref-crossref-type t))

    ;; These macros aren't used particularly often during the course of
    ;; normal referencing.
@@ -79,26 +109,48 @@ string."
     "namecref" "nameCref" "lcnamecref" "namecrefs" "nameCrefs"
     "lcnamecrefs" "labelcref" "labelcpageref")

+   ;; Add \label[type]{label} to AUCTeX parser
+   (TeX-auto-add-regexp LaTeX-cleveref-label-regexp)
+
+   ;; Tell RefTeX.  Check if `reftex-label-regexps' is bound and use a
+   ;; local version of it.  Check if the regexp is already added in
+   ;; order not to run `reftex-compile-variables' every time the style
+   ;; hook runs
+   (when (boundp 'reftex-label-regexps)
+     (unless (member "\\\\label\\[[^]]*\\]{\\(?1:[^\n\r%\\{}]+\\)}"
+                    reftex-label-regexps)
+       (add-to-list (make-local-variable 'reftex-label-regexps)
+                   "\\\\label\\[[^]]*\\]{\\(?1:[^\n\r%\\{}]+\\)}"
+                   t)
+       (reftex-compile-variables)))
+
    ;; Fontification
    (when (and (fboundp 'font-latex-add-keywords)
              (eq TeX-install-font-lock 'font-latex-setup))
-     (font-latex-add-keywords '(("cref" "*{")
-                               ("Cref" "*{")
-                               ("crefrange" "*{{")
-                               ("Crefrange" "*{{")
-                                ("cpageref" "{")
-                                ("Cpageref" "{")
-                                ("cpagerefrange" "{{")
-                                ("Cpagerefrange" "{{")
-                                ("namecref" "{")
-                                ("nameCref" "{")
-                                ("lcnamecref" "{")
-                                ("namecrefs" "{")
-                                ("nameCrefs" "{")
-                                ("lcnamecrefs" "{")
-                                ("labelcref" "{")
-                                ("labelcpageref" "{"))
-                             'reference))
+     (font-latex-add-keywords '(("cref"          "*{")
+                               ("Cref"          "*{")
+                               ("crefrange"     "*{{")
+                               ("Crefrange"     "*{{")
+                               ("cpageref"      "{")
+                               ("Cpageref"      "{")
+                               ("cpagerefrange" "{{")
+                               ("Cpagerefrange" "{{")
+                               ("namecref"      "{")
+                               ("nameCref"      "{")
+                               ("lcnamecref"    "{")
+                               ("namecrefs"     "{")
+                               ("nameCrefs"     "{")
+                               ("lcnamecrefs"   "{")
+                               ("labelcref"     "{")
+                               ("labelcpageref" "{")
+                               ("label"         "[{"))
+                             'reference)
+     (font-latex-add-keywords '(("crefalias"            "{{")
+                               ("crefname"             "{{{")
+                               ("Crefname"             "{{{")
+                               ("creflabelformat"      "{{")
+                               ("crefrangelabelformat" "{{"))
+                             'function))

    ;; Activate RefTeX reference style.
    (and LaTeX-reftex-ref-style-auto-activate
--8<---------------cut here---------------end--------------->8---

Best, Arash

Information forwarded to bug-auctex <at> gnu.org:
bug#36111; Package auctex. (Tue, 11 Jun 2019 17:41:02 GMT) Full text and rfc822 format available.

Message #11 received at 36111 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: Ryan Kavanagh <rak <at> debian.org>
Cc: 36111 <at> debbugs.gnu.org
Subject: Re: bug#36111: Support cleveref labels in AUCTeX and RefTeX
Date: Mon, 10 Jun 2019 21:40:18 +0200
Ryan Kavanagh <rak <at> debian.org> writes:

Hi Ryan,

thanks for your response.  Please keep 36111 <at> debbugs.gnu.org in Cc, thanks.

> I saved the attached file to
>
>     /home/rak/.emacs.d/tex-styles/cleveref.el
>
> I added
>
>     (setq TeX-style-private '("/home/rak/.emacs.d/tex-styles"))
>
> to my ~/.emacs and after restarting, describe-variable shows that this
> change took effect. The file also seems to be loaded:
>
>     TeX-active-styles is a variable defined in ‘tex.el’.
>     Its value is ("cleveref" "plain10" "plain" "latex2e" "f" "LATEX")

I don't have complete TeXlive installed, but is there a plain.cls
available somewhere?  It doesn't make a difference for this case, was
just curious.

> Unfortunately, it does not seem to work. Moving my cursor to "HERE" and
> calling reftex-label inserts "\label{eq:1}" instead of "\label{eq:2}".
> Here is my test file.
>
> ------8<------
> \documentclass{plain}
> \usepackage{cleveref}
> \crefname{test}{test}{test}
> \begin{document}
> \begin{equation}
>   \label[test]{eq:1}
> \end{equation}
> \begin{equation}
>   HERE
> \end{equation}
> \end{document}
> ------8<------

Your example above works for me.  I suspect that AUCTeX is still loading
the old style.  Can you please restart Emacs, load your test .tex file
and hit `C-h e', and check from which directory the file cleveref.el is
loaded?

> On the off-chance it was due to interference from the bundled copies of
> cleveref.el, the following changes of permissions didn't help:
> mode of '/home/rak/.emacs.d/elpa/auctex-12.1.2/style/cleveref.el' changed from 0644 (rw-r--r--) to 0000 (---------)
> mode of '/home/rak/.emacs.d/elpa/auctex-12.1.2/style/cleveref.elc' changed from 0644 (rw-r--r--) to 0000 (---------)

See above, I don't think permissions are relevant here (besides
reading).

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#36111; Package auctex. (Wed, 12 Jun 2019 13:52:02 GMT) Full text and rfc822 format available.

Message #14 received at 36111 <at> debbugs.gnu.org (full text, mbox):

From: Ryan Kavanagh <rak <at> debian.org>
To: Arash Esbati <arash <at> gnu.org>
Cc: 36111 <at> debbugs.gnu.org
Subject: Re: bug#36111: Support cleveref labels in AUCTeX and RefTeX
Date: Wed, 12 Jun 2019 09:51:16 -0400
[Message part 1 (text/plain, inline)]
On Mon, Jun 10, 2019 at 09:40:18PM +0200, Arash Esbati wrote:
> I don't have complete TeXlive installed, but is there a plain.cls
> available somewhere?

Sorry, I hadn't actually run LaTeX on that test file, and was just using
it for AUCTeX debugging. I somehow remembered the "minimal" class as
being called "plain". Replacing
    \documentclass{plain}
by
    \documentclass{minimal}
will get it compiling.

> Your example above works for me.  I suspect that AUCTeX is still loading
> the old style.  Can you please restart Emacs, load your test .tex file
> and hit `C-h e', and check from which directory the file cleveref.el is
> loaded?

Aha, that's the problem (and thanks for teaching me about `C-h e'):

Loading /home/rak/.emacs.d/elpa/auctex-12.1.2/style/cleveref.elc...done

The problem seems to be that I was setting TeX-style-private at the
bottom of my .emacs, after the stuff loading AUCTeX. Moving the setting
to the start of my ~/.emacs resulted in:

Loading /home/rak/.emacs.d/elpa/auctex-12.1.2/style/cleveref.elc...done

The new cleveref.el you provided fixes the bug. Thanks!

Best,
Ryan

-- 
|)|/  Ryan Kavanagh      | GPG: 4E46 9519 ED67 7734 268F
|\|\  https://rak.ac     |      BD95 8F7B F8FC 4A11 C97A
[signature.asc (application/pgp-signature, inline)]

Reply sent to Arash Esbati <arash <at> gnu.org>:
You have taken responsibility. (Sat, 15 Jun 2019 20:02:02 GMT) Full text and rfc822 format available.

Notification sent to Ryan Kavanagh <rak <at> debian.org>:
bug acknowledged by developer. (Sat, 15 Jun 2019 20:02:02 GMT) Full text and rfc822 format available.

Message #19 received at 36111-done <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: Ryan Kavanagh <rak <at> debian.org>
Cc: 36111-done <at> debbugs.gnu.org
Subject: Re: bug#36111: Support cleveref labels in AUCTeX and RefTeX
Date: Sat, 15 Jun 2019 22:01:30 +0200
Ryan Kavanagh <rak <at> debian.org> writes:

> The new cleveref.el you provided fixes the bug. Thanks!

Thanks for reporting this bug and testing the fix.  I've pushed a change
to AUCTeX repo -- closing this one.

Best, Arash




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 14 Jul 2019 11:24:07 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 259 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.