vim latex: open a pdf in zotero from the citation in the vim latex document.

I want to be able to open a pdf from my editor (vim) by having the cursor on the citation and running this python script (through a vim function that calls it).

I have this python script:
import sys
import requests
import json
import webbrowser

# get the citation key from the command line arguments
citation_key = sys.argv[1]

response = requests.post(
'http://localhost:23119/better-bibtex/json-rpc',
headers={'Content-Type': 'application/json'},
data=json.dumps({
'jsonrpc': '2.0',
'method': 'item',
'params': [{'citekey': citation_key}],
'id': 0,
})
)
print('------------------------')
print('this is the result of response.json(): \n',response.json())
item = response.json()['result']
attachment_path = item['attachments'][0]['path']

# this will open the attachment in the default PDF viewer
webbrowser.open(attachment_path)


But when I run it I get this error:
this is the result of response.json():
{'jsonrpc': '2.0', 'error': {'code': -32601, 'message': 'Method not found: item'}, 'id': None}
Traceback (most recent call last):
File "./openpdf_from_citation.py", line 47, in
item = response.json()['result']
~~~~~~~~~~~~~~~^^^^^^^^^^
KeyError: 'result'

This is what chatgpt had to say about this:

"The error message "Method not found: item" suggests that the Better BibTeX JSON-RPC server does not recognize the 'item' method.

As of my knowledge cutoff in September 2021, the Better BibTeX JSON-RPC server did not provide a method named 'item' for fetching a specific item by its citation key. It's possible that this method was added in a more recent version, or that there is some other method available for fetching items."

Can you help me here? Thanks.
  • Maybe I should provide some context:
    In order to check a citation in my document, what I have to do currently is:
    1. copy the citation
    2. go into zotero
    3. click on search
    4. click on the dropdown box under "Match the following"
    5. select citation key
    6. paste in the box
    7. double click the result
    8. double click the paper to open the pdf in the default pdf viewer.

    I was hoping to streamline this a bit, hence the above code.
  • I'm guessing there is no zotero.sqlite database connection between itemID and the 'citation key', is that right?
  • edited June 13, 2023
    We made a work around by making a bash script using the bib file:

    #!/bin/bash

    BIBTEX_FILE="/path/to/file.bib"

    open_pdf_by_citation_key() {
    CITATION_KEY="$1"

    # Check if the citation key exists in the .bib file
    if ! grep -q "@.*{${CITATION_KEY}," "$BIBTEX_FILE"; then
    echo "Error: Citation key not found"
    exit 1
    fi

    # Get the line with the file path from the .bib file
    LINE_WITH_FILE=$(grep -A20 "@.*{${CITATION_KEY}," "$BIBTEX_FILE" | grep "file =")

    echo "Line with file: $LINE_WITH_FILE"

    # Extract the file path from the line, keeping spaces but removing curly braces
    FILENAME=$(echo "$LINE_WITH_FILE" | cut -d'=' -f 2 | tr -d "{}")

    # Trim leading and trailing whitespace from FILENAME
    FILENAME=$(echo "$FILENAME" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')

    echo "Filename from .bib file: $FILENAME"

    # Use the FILENAME directly as it's a full path
    ABSOLUTE_PATH="$FILENAME"

    echo "Absolute path: $ABSOLUTE_PATH"

    xdg-open "$ABSOLUTE_PATH"
    }

    Then a function in the vimrc makes it so that typing ;oc in normal mode while the cursor is on the citation, opens the pdf that the citation refers to:

    function! OpenCitation(citation_key)
    execute "!bash /path/to/shellscript/ozp.sh " . a:citation_key
    endfunction

    command! -nargs=1 OpenCitation call OpenCitation(f-args)

    # I use ';' as the leader. Note the "cword" below needs to have less than and greater than signs on either side inside the quotes (but they didn't show up in this comment). Same with the f-args above.
    nnoremap ;oc :call OpenCitation(expand("cword"))
Sign In or Register to comment.