Need help getting noter/pdfs and org-roam-bibtex to work

So… depending on how you try to open a linked PDF, a different function gets triggered…

This operation below as originally reported should already work with the changes so far then:

I didn’t realise the small screen image below was for clicking a cite link for Org-ref… and it would call a different function.

1 Like

Although C-c ] can be set to anything of course, I doubt it calls org-roam-bibtex (there is no such function in ORB package). By default, org-ref-insert-link is bound to C-c ], which inserts a cite link by default. It can even some other things such open a PDF file, à la helm-bibtex (or ivy-bibtex), but org-ref-insert-link and helm-bibtex are not the same!

I think the author is just confused by the variety of packages, which is of no surprise. @sinqbad early Emacs experience can be overwhelming and one needs some time to get used - slowly but steadily. In your Emacs journey, C-h f, C-h v and C-h k are your best friends.

@sinqbad

I now seem to be able to reproduce the whole problem. It’s Org-ref… I have been using Helm-bibtex’s functions to open PDF documents. See the screen shot (this should work for you now).

While in the Helm completion (via C-]), while selecting the bib entry, pressing Tab gets you to this screen. The second function [f2] is the one I have been talking about.

Clicking on the cite:citeky gets you to this screen, which I didn’t understand that you had been talking about all along (apologies for this misintepretation). It does not find the PDF files linked to bib entry in my machine, either.

As mshevchuk’s analysis, this “2. Open pdf” option calls function org-ref-open-pdf-at-point (Org-ref function), and cannot find my PDF (via function org-ref-get-pdf-filename, which returns nil).

@mshevchuk

This does not seem to change the function used. I’ll dig deeper. There might be a configuration option to choose different methods (not sure)…

@nobiot

I just checked, it works for me.

MWE:
Evaluate

(setq org-ref-get-pdf-filename-function #'org-ref-get-pdf-filename-helm-bibtex)

BibTeX entry:

@article{citekey,
	File = {~/Public/article.pdf},
}

Org-mode entry

cite:citekey

@sinqbad @mshevchuk

This below works in my PC.

(defun my/org-ref-open-pdf-at-point ()
  "Open the pdf for bibtex key under point if it exists."
  (interactive)
  (let* ((results (org-ref-get-bibtex-key-and-file))
         (key (car results))
	 (pdf-file (car (bibtex-completion-find-pdf key))))
    (if (file-exists-p pdf-file)
	(find-file pdf-file) ; original in org-ref-help, 
                         ; opens external viewer (org-open-file pdf-file)
      (message "No PDF found for %s" key))))

(setq org-ref-open-pdf-function #'my/org-ref-open-pdf-at-point)

M-x org-ref-help opens a detailed help document on Org-ref.
In Appendex, there is a section named “Customizing how PDFs are opened”, section “A note for Mendeley, JabRef and Zotero users”. There is a sample configuration for Mendeley as well.

I think I’ll update the section of Zero to Emacs with this.

1 Like

Wow. I’m curious. Need to start to work; I’ll get back to this after work :wink:

It doesn’t. The function org-ref-open-pdf-at-point is still the one used. However, to get the filename, this function calls the function specified in org-ref-get-pdf-filename, which is just a variable to hold the name of the actual function. Setting org-ref-get-pdf-filename-function to org-ref-get-pdf-filename-helm-bibtex changes the way filenames are retrieved, in a way similar to what you suggested in my/org-ref-open-pdf-at-point. The latter is of course a valid solution, moreover it is more flexible since the user is in the full control.

Sorry, the name of the variable to set is org-ref-get-filename-function. This explains it - I used the correct variable when testing but made a mistake when reporting, sorry for the mess. I updated my answers accordingly.

Your solution opens up a PDF document in an external viewer.
Function org-ref-open-pdf-at-point uses org-open-file, which seems to call an external viewer by default for PDF files, which you can customize…

It’s a rabbit hole; I can finally see the end of the tunnel :wink:

This thread of conversations has a lot of gems in it, but getting really crowded.

Sorry, @sinqbad, you would be hard pressed to decipher a solution that would work in the end, but this should be it (I might separate the solutions out as a tips post).

Two options, if you want to open PDF documents from the cite:citekey link of Org-ref:

  1. Use the way described in org-ref-help and adapt the code to call find-file (code quoted again below)

  2. Follow @mshevchuk’s solution: (setq org-ref-get-pdf-filename-function #'org-ref-get-pdf-filename-helm-bibtex) and customize variable org-file-apps so that org-open-file function uses find-file to open PDF files

Option 2 above also works:

1 Like

I have a question on this topic. (please let me know if I need to open a new thread)

When I click the cite:key, I have the same interface as @nobiot . I think it’s a helm related function, but I could not locatate the exact function.

My motivation is to change the default action after I click the cite:key (or C-c C-o org-open-at-point). This is what I use most.

@nobiot actions list are a little bit different to me. But I find the following code might be help me to solve this problem.

(helm :sources (helm-build-sync-source "test"
                     ;; A function can be used as well
                     ;; to provide candidates.
                     :candidates '( "bar" "baz" "foo"))
          :buffer "*helm test*")

After executing this funciton, I find the buffer name is “helm test”.

So, I guess, I could start from the name of buffer, which is defined by :buffer to find the function.

However, I could not located it.

I searched the following elisp code:

  • org-ref (org-ref-helm-bibtex.el)
    (:buffer "*helm browser*")

  • helm-bibtex
    :buffer "*helm bibtex*"

None of them has a string like this: :buffer "*helm*".

Not solving this issue right now, but I will continue.

Hmm! In the end, the two options are the same.

You just need to either change the code, or customize org-file-apps to set to find-fiie for org-open-file function.

This whole thing needs to be a separate post…

1 Like

@cool_ran, yes, I think it would be a good separate post.
I believe you can find an answer to your question by reading org-ref-help document… (it’s a command, so M-x org-ref-help).

Thanks @nobiot on org-ref-help, I will do my work and a new thread if necessary.

1 Like

When you open a cite:key link, the function currently registered for that link pops up. You can see all link types that Org is aware of in org-link-parameters variable. Org Ref links such as cite: and co. (autocite:, nocite:, etc.) call a function registered in org-ref-cite-onclick-function variable. It can be set before org-ref is loaded to support different completion styles. The default one and the one you are using is org-ref-helm-bibtex.

I don’t use Helm, so I can’t tell whether what you want can be set from within the Helm framework. Quite likely, you have to inspect code of the org-ref-helm-bibtex function and adjust it or/and other relevant pieces to your needs. I’m afraid it may be hard-coded, but can’t tell it for sure.

1 Like

Thank you @mshevchuk. You point me to the right direction org-ref-cite-onclick-function.

I hard code org-ref-cite-click-helm to achieve my wish.

Now, I can C-c C-o and RET to open a pdf.

Also, thank you @nobiot for helping out.

Following is my tweak:

;;;; Hard revise the sequence of `org-ref-cite-click-helm`
;; ref https://org-roam.discourse.group/t/need-help-getting-noter-pdfs-and-org-roam-bibtex-to-work/794/34

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (helm :sources `(                                                  ;;
;;                      ((name . "User functions")                    ;;
;;                       (candidates . ,org-ref-helm-user-candidates) ;;
;;                       (action . (lambda (f)                        ;;
;;                                   (switch-to-buffer ,cb)           ;;
;;                                   (funcall f))))                   ;;
;;                      ((name . ,name)                               ;;
;;                       (candidates . ,candidates)                   ;;
;;                       (action . (lambda (f)                        ;;
;;                                   (switch-to-buffer ,cb)           ;;
;;                                   (funcall f))))                   ;;
;;                      ))                                            ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'org-ref-helm-bibtex)

(defun my/org-ref-open-pdf-at-point ()
  "Open the pdf for bibtex key under point if it exists."
  (interactive)
  (let* ((results (org-ref-get-bibtex-key-and-file))
         (key (car results))
	 (pdf-file (car (bibtex-completion-find-pdf key))))
    (if (file-exists-p pdf-file)
	(find-file pdf-file) ; original in org-ref-help,
                                        ; opens external viewer (org-open-file pdf-file)
      (message "No PDF found for %s" key))))

(setq org-ref-open-pdf-function #'my/org-ref-open-pdf-at-point)

(add-to-list
 'org-ref-helm-user-candidates
 '("Quick Open PDF" . (lambda ()
                              (my/org-ref-open-pdf-at-point)))
 nil)

(defun org-ref-cite-click-helm (_key)
  "Open helm for actions on a cite link.
subtle points.

1. get name and candidates before entering helm because we need
the org-buffer.

2. switch back to the org buffer before evaluating the
action.  most of them need the point and buffer.

KEY is returned for the selected item(s) in helm."
  (interactive)
  (let ((name (org-ref-format-entry (org-ref-get-bibtex-key-under-cursor)))
        (candidates (org-ref-cite-candidates))
        (cb (current-buffer)))

    (helm :sources `(
                     ((name . "User functions")
                      (candidates . ,org-ref-helm-user-candidates)
                      (action . (lambda (f)
                                  (switch-to-buffer ,cb)
                                  (funcall f))))
                     ((name . ,name)
                      (candidates . ,candidates)
                      (action . (lambda (f)
                                  (switch-to-buffer ,cb)
                                  (funcall f))))
                     ))))

(provide 'wr-tweak-org-ref)

2 Likes

Yeah, although Org Ref is a very tricky package requiring a lot of struggle to properly run it, it is at the same time highly customizable, allowing one to plug in their custom functions almost everywhere without touching the core code.

1 Like

It’s like jumping in the deep end. Rewarding once you figure it out but frustrating when you can’t crack it. But thank you for all your help. Makes it much easier to dive in.

Thank you for taking the time to write and explain that to me. It really does help to contextulise and expand my knowledge.

I see what I am doing wrong now thank to both @mshevchuk and @nobiot.

Seriously, thank you so much for all the work you’ve put into this. I tried your solution code and it worked! It only worked with the absolute path entry that I had manually put in. Otherwise, with a relative path entry it still will fail to open anything.

That leads me to believe I’m in fact having two problems and mistaking them for the same problem. That is 1) I was using org-ref “2. Open pdf” org-ref-open-pdf-at-point rather than the helm-bibtex menu (C-c ] ) and 2) something wrong with relative path OR the naming of the pdfs.

Yes I just did.


“[f2] Open PDF, URL or DOI” still won’t open the pdf. I think it’s because of what bibtex-completion-library-path says, that “Bibtex-completion assumes that the names of these PDFs are composed of the BibTeX-key plus a “.pdf” suffix.”
So I’m guessing that I have to get zotero to export appropriately. I’ll test out it out.

Thanks @nobiot and @mshevchuk for all your help.

EDIT: Actually, I don’t think the Bibtex-key plus pdf file name is the issue given that @nobiot’s function works.

1 Like

I will try doing this. However, how do I edebug-defun the function bibtex-completion-open-pdf. When I type M-x edebug-defun says “Cannot more futher down” i.e. I think it’s acting on the buffer.

You need to be in the source code where the function is defined – you cursor needs to be somewhere in the defun for the function. (defun is the keyword to define a function). You can easily navigate to where you need to be via C-h f.

You can remove your edebug point with a commend something like edebug remove instrument or something like this.

I figured out why the relative paths aren’t being parsed properly. According to this https://github.com/tmalsburg/helm-bibtex/issues/124 issue, having the file field in a jabref format solves the issue. Eg.

@article{wolfmoon12,
  title = {"{{The Moon}} over the {{Mountain}}": {{Stories}} by {{Atsushi Nakajima}} by {{Paul McCarthy}} and {{Nobuko Ochner}} (Review)},
  shorttitle = {"{{The Moon}} over the {{Mountain}}"},
  author = {Wolf, Charles De},
  date = {2012},
  journaltitle = {Monumenta Nipponica},
  volume = {67},
  pages = {371--374},
  publisher = {{Sophia University}},
  issn = {1880-1390},
  doi = {10.1353/mni.2012.0021},
  file = {Full Text PDF:files/1450/Wolf - 2012 - The Moon over the Mountain Stories by Atsushi N.pdf:application/pdf},
  langid = {english},
  number = {2},
  timestamp = {2020-06-27T04:55:41Z}
} 

Notice the file field is very different than earlier in the thread.

I use Zotero as my bibliography management and betterbib(la)tex to export. To export the bibtex file with the jabref go to Zotero > Edit > Preferences > Better BibTeX > export > Fields > Include JabRef-specific metadata:

Once in jabref format bibtex-helm is able to load the pdf.

2 Likes