I'm not even sure I'm asking the right question, but here goes: I followed this guide which introduces a function to search an org-mode file for a reftex/bibtex key and jump to that position in the file. The function is as follows:

(defun org-mode-reftex-search ()
  ;;jump to the notes for the paper pointed to at from reftex search
  (org-open-link-from-string (format "[[notes:%s]]" (first (reftex-citation t)))))

When I use the function and search reftex for a file that is in the buffer, everything works beautifully. When I search for a file that is not in the buffer, however, I get asked No match - create this as a new heading? (y or n). If I say y, a new top level entry is put into the buffer with the format:

* #BibtexKey

The problem is that I've taken the time to set up a better format for new entries in my org-mode notes file. What I would like is to use that format when the entry doesn't already exist. The format I'm using, again from that tutorial, is:

** %t
  :Custom_ID: %l

Where %t is the title of the paper and %l is the Bibtex key. How do I replace the default format of a reftex search?


Inserting a new heading is actually done by org-link-search. The relevant part of the function (currently) looks like this:

((and (not org-link-search-inhibit-query)
      (eq org-link-search-must-match-exact-headline 'query-to-create)
      (y-or-n-p "No match - create this as a new heading? "))
    (goto-char (point-max))
    (unless (bolp) (newline))
    (org-insert-heading nil t t)
    (insert s "\n")
    (beginning-of-line 0))

So there is no easy way to hook into it and produce the format you want. You can set org-link-search-must-match-exact-headline to t, but then you'll have to make the heading manually, so that doesn't get you to where you want. One option is to catch the error after calling org-open-link-from-string (with org-link-search-must-match-exact-headline set to t). Here's one (maybe not-so-elegant) way to do that.

(defun org-mode-reftex-search ()
  (let ((reftex-default-bibliography
         (mapcar #'expand-file-name reftex-default-bibliography))
        tree key)
    (setq tree
            (let ((reftex-cite-format "** %t\n  :PROPERTIES:\n  :CUSTOM_ID: %l\n  :END:\n[[papers:%l][%l-paper]]"))
              (setq key (car (reftex-citation)))
    (condition-case nil
        (let ((org-link-search-must-match-exact-headline t))
          (org-open-link-from-string (format "file:/tmp/notes.org::#%s" key)))
       (with-current-buffer (get-file-buffer "/tmp/notes.org")
         (goto-char (point-max))
         (unless (bolp) (newline))
         (insert tree)

I think the only thing you should have to modify in the code above is the path to your notes.

  • Running this gives me: No valid bibliography in this document, and no default available. I define the bibliography at the top of the notes.org file with # \bibliography{refs.bib}. – Ryan Aug 27 '15 at 15:47

Your Answer

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

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