3

Whenever I insert a reference to a label starting with eq: RefTeX seems to decide to surround it with (), e.g. it will insert (\ref{eq:1)) into the buffer. This is particularly annoying if I am using cleveref, because (\cref{eq:1}) is not at all right.

It is implied in one other place (the only place I've seen it mentioned) that this surprising/unexpected behaviour and may be due to my own customisations (of which there aren't many at all)... but if I start emacs with emacs -Q to disable all of them, it still occurs for both of the built-in reference formats (\ref and \pageref).

I'm using Emacs 24.5 with RefTex 24.4 on Ubuntu.

It would be very nice to stop/control this behaviour.

huon
  • 2,132
  • 1
  • 13
  • 14

1 Answers1

4

After a bit of testing, I stand corrected (with respect what I said in the answer you mentioned): what you describe is indeed the default behavior, but of course can be changed. If you want to use amsmath style of referecen, add the following code to your init file:

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

as also suggested in the RefTeX manual.

If in particular you want to use \eqref{} when cleveref is not loaded, and not to have anyway the parentheses around \ref{} in documents loading cleveref package add the following code to your init file (it's based on this answer, already mentioned in the question)

(setq reftex-label-alist '(AMSTeX)) ;; as before, but will be changed in cleveref document, see below

(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")
      (set (make-variable-buffer-local 'reftex-label-alist)
       '(("equation"  ?e "eq:" "~\\ref{%s}" t
          (regexp "equations?" "eqs?\\." "eqn\\." "Gleichung\\(en\\)?"  "Gl\\."))))
      (TeX-add-symbols
       '("cref" TeX-arg-ref)
       '("Cref" TeX-arg-ref)
       '("cpageref" TeX-arg-ref)
       '("Cpageref" TeX-arg-ref)))))
giordano
  • 8,486
  • Hm, so this improves/changes things somewhat: instead of (...), it uses eqref for eq:... labels (even if I try to use non-ref reference formats like pageref or the cref one I've defined). – huon Nov 13 '15 at 11:03
  • @huon See the updated answer. – giordano Nov 13 '15 at 11:28
  • Thanks! However, I'm still not sure that's really the right thing. I want RefTeX to understand that when I explicitly request the \cref format (added as you suggest in the answer that I linked in my question), it should just insert \cref instead of trying to guess what I mean with the heuristics that don't apply to the more intelligent referencing packages. I also don't really want to force the \cref format (not all the documents I work on use it). – huon Nov 13 '15 at 11:46
  • (Lastly, it's not just the e key: this seems to be driven by the eq: prefix, for instance, if I use space to see all the labels in my file, RefTeX still inserts parens/eqref if I choose an eq:... label.) – huon Nov 13 '15 at 11:48
  • Indeed reftex-typekey-to-format-alist is relevant not only when the e type of labels is selected, but every time a label with eq: prefix is chosen. If in a document with cleveref you always want to you only \cref you only need to set reftex-label-alist to nil there (add (set (make-variable-buffer-local 'reftex-label-alist) nil) in the hook of the other question, after reftex-ref-style-activate) – giordano Nov 13 '15 at 12:49
  • Ok. If I use your next suggestion, that still seems to insert parens, e.g. my reftex-related config looks like https://gist.github.com/huonw/2b8cf14507217760cd78 but using c (i.e. cleveref) with an eq: label still has surrounding parens. (reftex-label-alist is indeed nil, per C-h v, but that also tells me the global value is nil so that doesn't seem to have changed the situation.) – huon Nov 14 '15 at 08:13
  • But did you also add the line (setq reftex-label-alist '(AMSTeX)) in your init file? – giordano Nov 14 '15 at 14:09
  • Oh, I didn't realise it was meant to be an addition to that. However, that doesn't actually change anything: I still get (\cref{eq:1}), i.e. parens. – huon Nov 14 '15 at 23:07
  • I guess you didn't put the (add-to-list 'reftex-typekey-to-format-alist '("e" . "~\\ref{%s}")) line your init file, either. – giordano Nov 15 '15 at 18:13
  • Sorry, I forgot to mention that I tried it, and it doesn't seem to do anything: parens are still included. – huon Nov 15 '15 at 23:41
  • Having a look to your configuration would help now. – giordano Nov 16 '15 at 09:00
  • In https://gist.github.com/huonw/a7615ddb1dd2868f952a , I started with a plain init.el file with each of the suggestions (as I've understood them), and tested them, and none worked. Do you see different behaviour? (I guess it's not the worst to have equations use \eqref and everything else use cref: I can just alias the former to the latter, but it'd be nice to not have to do it.) – huon Nov 17 '15 at 12:25
  • RefTeX's code is somewhat evil, the trick with reftex-typekey-to-format-alist seems to work only in some conditions (the ones I was testing, of course). I updated the answer, should work now. – giordano Nov 17 '15 at 21:04
  • Awesome, \cref is now inserted without parens! However it seems to be unconditionally running the cleveref style hook, even in a document without cleveref. See init6.el. – huon Nov 17 '15 at 23:55
  • So if you open a document without cleveref package, the variable TeX-active-styles contains "cleveref" (issue C-h v TeX-active-styles RET in that buffer)? – giordano Nov 18 '15 at 10:05
  • Oh, the problem was I was using the same document, just adding/removing the \usepackage{cleveref} and then closing/reopening emacs, but the caching in auto/*.el meant it was sticking with the old list of packages. It works fine if I delete that file, or if I use separate files like the gist suggested. Thanks so much for working through this with me! – huon Nov 18 '15 at 11:23
  • You just need to issue C-c C-n to force reparsing of the file ;-) – giordano Nov 18 '15 at 12:10