# AUCTeX does not recognize cleveref labels

I use the cleveref package, which allows annotating \label commands with an optional argument. Unfortunately, this prevents the various functions in latex.el from detecting those labels. My guess is that it is because they do not match the built-in regexp for labels.

Some example LaTeX code to illustrate the situation:

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


AUCTeX will not notice that I have used eq:23, and then if I try to automatically generate labels, it will again propose eq:23.

Digging around in the AUCTeX sources, I found the following in latex.el:

(defvar LaTeX-auto-label-regexp-list
'(("\\\\label{\$$[^^M\n\r%\\{}]+\$$}" 1 LaTeX-auto-label))
"List of regular expression matching LaTeX labels only.")


This seems to be the appropriate variable to extend. However, I can't seem to get the regexp right. I have tried adding the following to my emacs config before AUCTeX gets loaded (and it appears in the output of describe-variable for LaTeX-auto-label-regexp-list), but AUCTeX is still not recognizing the above as labels.

(setq LaTeX-auto-label-regexp-list
'(("\\\\label{\$$[^^M%\\{}]+\$$}" 1 LaTeX-auto-label)
("\\\\label\$[^^M[\\{}]+\${\$$[^^M%\\{}]+\$$}" 1 LaTeX-auto-label)))


What am I missing?

• Not at my computer, but try moving your first ^M right after the following [ (in the regexp you added). Jun 5, 2019 at 16:11
• Thanks for the hint, but no luck. I built the regexp using emacs's re-builder, and it correctly matches \label[abc]{eq:3} for example. Jun 5, 2019 at 16:32
• I wonder if it's really AUCTeX that is automatically generating your labels. How do you do it? Aren't you using RefTeX? If so, maybe LaTeX-auto-label-regexp-list is used for something else in AUCTeX, such as fontification. Jun 5, 2019 at 19:38
• Good catch @frougon . RefTeX's reftex-label function is what seems to actually generate the labels, and I think it uses reftex-parse-from-file to figure out existing labels. Unfortunately, it hard-codes the \label{ prefix. I've filed a wishlist bug report. I'll update the question with an answer if ever it gets fixed. Jun 6, 2019 at 2:03

Within AUCTeX, latex.el provides support for vanilla LaTeX. Additions by packages are catered via AUCTeX style files. In this case, this is a shortcoming in cleveref.el which didn't have support for an optional argument in \label macro. This issue is now fixed and will be available with next AUCTeX release.
If you want to use it now, customize the variable TeX-style-private to a directory of your choice and save the updated style there. Restart Emacs and AUCTeX will use the new version. Just keep in mind to remove it once a new AUCTeX version is out.