I use org-mode to write a paper, with RefTeX as minor-mode to insert quotations from a BibTeX (.bib) file.

RefTeX needs to be told which .bib file to use. This is done by assigning a value to reftex-default-bibliography.

I would like a .bib file to be automatically assigned, depending on the name of the file I am editing. For example, if I edit a paper called the-life-of-ants.org, RefTeX should automatically load the-life-of-ants.bib, stored in the same folder as the .org file.

If no .bib file with the same name as the .org file is stored in the local directory, my default bibliography file should be loaded instead.

This is in Emacs 25.3.1 on Windows 7.

I've tried a function that is called whenever org-mode is loaded, using a hook in the last line of the code below. (The code also does some unrelated stuff, like changing the default keybinding for inserting citations to make it compatible with org-mode standard bindings.)

(defun org-mode-reftex-setup ()
(and (buffer-file-name)
(file-exists-p (buffer-file-name))
(reftex-parse-all))
(define-key org-mode-map (kbd "C-c (") 'reftex-citation) ; key-binding
(if (file-exists-p (concat (file-name-sans-extension buffer-file-name) ".bib")) ; either use local .bib file
(progn
(setq reftex-default-bibliography
(convert-standard-filename (concat (file-name-sans-extension buffer-file-name) ".bib")))
(message "Local bib-file loaded.")) ; or use default .bib file
(progn
(setq reftex-default-bibliography
'"c:\\foo\\bar\\baz\\References.bib")
(message "No bib-file found. Using default bibliography."))))



Upon visiting an Org file, the function is executed as expected, and the correct value (it seems) is assigned to reftex-default-bibliography -- or is it?

M-x eval-expression (message reftex-default-bibliography) tells me:

c:\foo\bar\local-bibliography.bib
"c:\\foo\\bar\\local-bibliography.bib"


I don't understand why two lines are produced, one with quotes and double-backslashes (which are needed, I think, to correctly parse file-paths in MS Windows, and which are produced by convert-standard-filename), and one without.

However, M-x eval-expression reftex-default-bibliography tells me only:

"c:\\foo\\bar\\local-bibliography.bib"


which seems to be correct.

And yet, when I try to insert a reference, I get an error:

reftex-locate-file: Wrong type argument: stringp, 99

What could be the problem here?

• Why two lines are produced: message returns its argument and as a side effect prints that argument to the minibuffer (and the Messages buffer). The first line is the side-effect and the second is the argument being returned. – Fran Burstall Oct 19 '18 at 16:58
• It seems at some point, a function named reftex-locate-file is given 99 as an argument while it expects a string. You could (setq debug-on-error t) to get a stack trace and get more information. – JeanPierre Oct 21 '18 at 20:03