# Emacs, RefTeX and Cleveref – can they work together?

Trying to look for an answer in the net yielded only one questions and one answer which I could neither understand nor use. On tex.stackexchange I found only this question which was answered but I'm under-skilled to translate it to my case.

I want to use cleveref together with reftex when using AUCTeX in emacs. In other words, when pressing C-c ) and finding the label I want to refer to, I want reftex to insert \cref{label} instead of \ref{label}.

Probably there's a way to do it, but I couldn't figure it out.

-

The following (untested!) code should give you the behavior you are looking for. You need to put it in your Emacs initialization file (.emacs or some such, depending on your platform).

(defun reftex-format-cref (label def-fmt)
(format "\\cref{%s}" label))
(setq reftex-format-ref-function 'reftex-format-cref)

(The trivial code for the defun was modified from the reftex-ref.el file distributed with Emacs.)

-
This used to work for me, but since upgrading to Emacs 24.3 and auctex 11.87, it no longer works. Any suggestions? –  MTS Apr 7 at 23:45

Since you seem to already be using YASnippet it might be better to implement this via YASnippet instead of via AUCTeX.

Here is an example of such a snippet:

# -*- mode: snippet -*-
# name: cleveref \cref
# key: rf
# --
\cref{${1:label$(unless yas/modified-p (reftex-reference nil 'dont-insert))}}$0 Using this you write "rf" and when you expand it (default is to expand with Tab) RefTeX will ask you for what label to refer to and insert that label as an argument of \cref. One of the benefits of using YASnippets is that you can have more than one alternative for the same key. For example if you use the above snippet you may also want to use the following for the cases where the cross-reference starts a sentence: # -*- mode: snippet -*- # name: cleveref \Cref # key: crf # -- \Cref{${1:label$(unless yas/modified-p (reftex-reference nil 'dont-insert))}}$0

Now when you write "rf" and expand it you get to choose whether you want it to expand to \cref or \Cref:

For more cross-reference snippets see https://github.com/Sleft/yasnippet-latex-mode/tree/master/cross-reference.

-

Nowadays (RefTex version 24.3), the function is called with three parameters: label, def-fmt, and ref-style. You can safely ignore the new one, so this should work:

(defun reftex-format-cref (label def-fmt ref-style)
(format "\\cref{%s}" label))
(setq reftex-format-ref-function 'reftex-format-cref)

However, there is a better answer provided in this (duplicate) question.

-