Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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.)

share|improve this answer
1  
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

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.

share|improve this answer

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:

Choosing between \cref and \Cref in YASnippet

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.