1

The question is the same as in this other post, that I want C-c [ to insert ~\cite instead of just \cite. However, the solution given therein as follows

(setq reftex-cite-format
  '((?\C-m . "~\\cite{%l}")
    (?t    . "\\cite{%l}")))

somehow does not work for me.

I tried the suggestion in the mail list here,

(setq reftex-format-cite-function 
  '(lambda (key fmt)
     (let ((cite (replace-regexp-in-string "%l" key fmt)))
       (if (or (= ?~ (string-to-char fmt))
               (member (preceding-char) '(?\ ?\t ?\n ?~)))
           cite
         (concat "~" cite)))))

which does work for me, except the following complication, that when I already have something like ~\cite[xxx], and I would like to insert another citation, say, yyy. Given the cursor is placed as in ~\cite[xxx(cursor here)], if insert yyy using C-c [, I would then get ~\cite[xxx,~yyy], which certainly does not compile correctly.

Anything I can to to achieve insertion of ~ on first call of C-c [ and omit ~ on second and further call? Many thanks!


Update

Congratulations on my Tumbleweed badge earned from this post! (╯‵□′)╯︵┻━┻

I tried the following tweak of the mail list answer,

(setq reftex-format-cite-function 
  '(lambda (key fmt)
     (let ((cite (replace-regexp-in-string "%l" key fmt)))
       (if (or (= ?~ (string-to-char fmt))
               (member (preceding-char) '(?\ ?\t ?\n ?~ ?{ ?,))
               (member (following-char) '(?} ))
     )
           cite
         (concat "~" cite)))))

and it's working well so far. It seems that considering more preceding characters and also including following character helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.