8

How can I set up cleveref to work with AUCTeX and RefTeX? I have tried the advice on Emacs, RefTeX and Cleveref – can they work together? but it didn't work.

Additionally, when I hit enter, \ref{} portion is always enclosed in ( ). How can I get rid of ( ) enclosing the ref{}?

I also checked out:

AUCTeX – new commands recognized as such

this post but all the information is relatively old so maybe there is a newer answer?

dustin
  • 18,617
  • 23
  • 99
  • 204

1 Answers1

13

Add the following code to your init file

(eval-after-load
    "latex"
  '(TeX-add-style-hook
    "cleveref"
    (lambda ()
      (if (boundp 'reftex-ref-style-alist)
      (add-to-list
       'reftex-ref-style-alist
       '("Cleveref" "cleveref"
         (("\\cref" ?c) ("\\Cref" ?C) ("\\cpageref" ?d) ("\\Cpageref" ?D)))))
      (reftex-ref-style-activate "Cleveref")
      (TeX-add-symbols
       '("cref" TeX-arg-ref)
       '("Cref" TeX-arg-ref)
       '("cpageref" TeX-arg-ref)
       '("Cpageref" TeX-arg-ref)))))

I've taken the value for reftex-ref-style-alist from this revision: http://bzr.savannah.gnu.org/lh/emacs/trunk/revision/112689

For the \ref enclosed in ( ), add the following code to your init file:

(setq reftex-label-alist '(AMSTeX))

as also suggested in the RefTeX manual (see also Emacs' RefTeX reftex-reference unconditionally surrounding `eq:...` reference with parens)

giordano
  • 8,486
  • The code I posted works for me: if I press C-c C-m cref RET, I get \cref, not \ref, it's impossible you get a \ref. Please explain better what's your problem and, if you can, post all AUCTeX/RefTeX related customizations in your .emacs. – giordano Jun 15 '13 at 15:12
  • @dustin I updated my answer, but you didn't specify that. – giordano Jun 15 '13 at 15:38
  • @dustin which version of AUCTeX and RefTeX are you using? Here: AUCTeX 11.87 and RefTeX shipped with Emacs 24.3. Do you set AUCTeX TeX-parse-self and TeX-auto-save variables to t? – giordano Jun 19 '13 at 23:38
  • See the first lines of AUCTeX manual: http://www.gnu.org/software/auctex/manual/auctex.html If you want to exploit all AUCTeX features (in particular its ability to parse TeX documents) you have to set those variables to non-nil. My code works in that case, that's the most common case among AUCTeX users. – giordano Jun 19 '13 at 23:41
  • If TeX-parse-self and TeX-auto-save are non nil, it must work. When you have a \usepackage{cleveref} in your document cleveref appears in TeX-style-list variable (check its value in your buffer) and the hook associated to cleveref is run, that is the hook I wrote in the answer. – giordano Jun 19 '13 at 23:57
  • Sorry if I insist but it must works, if doesn't work it's a bug in AUCTeX, and you should report it, or there is something tricky in your configuration. Check the value of TeX-active-styles in the LaTeX buffer (the previous mentioned TeX-style-list doesn't exist, my fault): is "cleveref" present? Check the value of TeX-style-hook-list: is the hook I wrote present? – giordano Jun 20 '13 at 00:03
  • In general you can view the value of a variable with C-h v, so C-h v TeX-active-styles RET and C-h v TeX-style-hook-list RET (you can use auto completion with TAB). These variables are buffer local, so you must issue those commands in the same buffer of the document you are editing. – giordano Jun 20 '13 at 00:09
  • We are going a bit off topic here, but those variables are correctly set, perhaps there is something wrong with your AUCTeX/RefTeX configuration. You can try starting Emacs with a clean .emacs except for setting TeX-auto-save, TeX-parse-self to t and the code I posted in the answer. – giordano Jun 20 '13 at 10:36
  • I just uninstalled TeXlive2012 and installed 2013 and now your solutions works. I don't know how or why that would effect it though. – dustin Jun 20 '13 at 21:48
  • Me neither :-| That should be completely unrelated to the distribution you are using. – giordano Jun 20 '13 at 21:54
  • 1
    This doesn't seem to be working for me because the "cleveref" style isn't being set (it isn't in TeX-active-styles. I include the package with \usepackage[capitalise,noabbrev]{cleveref} in my master document, but reftex-ref-style-alist is never updated. How should the "cleveref" style become active? – robin Mar 19 '14 at 22:58
  • @robin Do you have parsing enabled? – giordano Mar 19 '14 at 23:13
  • @giordana I think so yes: TeX-parse-self, TeX-auto-save and reftex-plug-into-AUCTeX are set to t – robin Mar 20 '14 at 10:50
  • @giordano robin appears to have a subfile-based project, judging from "in my master document". I seem to run into the same issues as robin, as I also have a subfile based project and the cleverref style does not get activated… :/ Is there a way to forcefully enable the style? – Flow Apr 13 '23 at 11:52