I am using BibSonomy for my for managing my references and TeXlipse as my LaTeX editor. While the latter is arguably a little outdated, it still works well and provides some features which other editors seem to lack (e.g., partial compilation not supported by TeXstudio or TeXmaker). Anyways, what I wanted was a way to directly access my PDFs from TeXlipse. It turned out there is an easy (if hacky) way of achieving this (of course there are other, cleaner ways such as writing an extension or plug into the existing BibSonomy extension). So here is what is possible now:
Highlight the Bib-Key (such as becker2016sparktrails) and hit F9 (or any custom shortcut). Voilà the PDF opens up.
As I mentioned the process to achieve this is a little hacky, and has some caveats:
- It is based on the External Tools functionality built into Eclipse, i.e., we take advantage of the shortcut (F9) for the last used external tool meaning that if we use another external tool, we need to manually run our “Open PDF” external tool once, before the (F9) shortcut works again.
- We need to manually trigger the process of downloading the PDFs from BibSonomy (which need to be stored there in the first place, obviously).
Nevertheless, I found the process quite useful, so here is how to do it:
Howto
We will store our PDFs in a folder within our LaTeX project. I use build/papers . Then we use an external tool which uses the currently selected Bib-Key to open the appropriate PDF.
Downloading PDFs
For downloading the PDFs I use a Python 3 script which I have placed into a folder in my LaTeX project. I call this folder build . Paper are downloaded to build/papers . The python script is called as follows:
1 2 |
python download.py <username> <apikey> <comma separated tags> python download.py becker b6d046e8812a8bbad18892a45e33411f inthesis,diss |
Here is the Python 3 source code. Note that this code currently only supports downloading up to 999 publications due to limitations in the BibSonomy API. This could be fixed by implementing a recursive downloading procedure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import sys import urllib.parse import urllib.request import json import requests import re import os.path user = sys.argv[1] apikey = sys.argv[2] tags = " ".join(sys.argv[3].split(",")) print('Requesting (max 999) publications with tags "{}" from user "{}".'.format(tags, user)) url = "https://www.bibsonomy.org/api/users/{}/posts?resourcetype=bibtex&start=0&end=999&format=json&tags={}".format(user, urllib.parse.quote(tags)) response = requests.get(url, auth=(user,apikey)) if response.status_code is not requests.codes.ok: print('Failed to request data. Response can be found in "response.log".') with open("reponse.log", "wb") as f: f.write(response) exit() print('Request successful. Parsing response.') obj = json.loads(response.content.decode('utf-8')) print('Downloading publication PDFs.') posts = obj["posts"]["post"] print('Number of publications: {}', len(posts)) n_downloaded = 0 n_exists = 0 n_nopdf = 0 for p in posts: bibkey = re.sub(r'\W+', '', p["bibtex"]["bibtexKey"]) print("* {}".format(bibkey)) path = "papers/" + bibkey + ".pdf" if os.path.isfile(path): print(" Info: Publication already downloaded.") n_exists += 1 else: if "documents" in p and len(p["documents"]["document"]) > 0: document_url = p["documents"]["document"][0]["href"] # open in binary mode with open("papers/" + bibkey + ".pdf", "wb") as f: # get request response = requests.get(document_url, auth=(user,apikey)) # write to file f.write(response.content) print(" Success: Downloaded.") n_downloaded += 1 else: print(" Error: No document exists for this publication.") n_nopdf += 1 print() print("Statistics:") print("* Downloaded: ", n_downloaded) print("* Existed: ", n_exists) print("* No PDF: ", n_nopdf) |
Open PDFs
The configuration of the external tool ( Run -> External Tools -> External Tools Configuration ) is pretty easy, here my configuration using Okular as PDF viewer:
To configure a shortcut for this external tool, we use the Key Run Last Launched External Tool , which I set to F9. Here is a screenshot of my config:
Future Work
Here is some future work which may prove interesting, but I will probably never come around to do 😉
- Shortcuts for specific external tools (probably needs an extension) in order to replace the usage of the “last used external tool” key
- An extension for automatically downloading missing PDFs since we currently need to start this process by hand
- Extending the already existing BibSonomy plug-in for TeXlipse to create an integrated experience
- Adding something like this to TeXstudio or TeXmaker via JavaScript extension points, in order to support more recent TeX-editors