Making Zotero work with git and bibtex

03.02.2025

I have developed a very convenient approach to the bibtex bibliographies in git repos. In brief, it relies on setting the $BIBINPUTS environment variable, then using jabref.

First, the problem that I wanted to solve.

My library in Zotero has a few thousands of entries, and it is not practical to keep it in the git repo. One of the reasons is that I feel ashamed of my reference hoarding. Previously, I would create a folder in Zotero to get automatically exported to a bib in the project folder (this is possible using better bibtex addon). To have a new reference in this project bib file I’d need to manually drag this reference into the proper Zotero folder. Writing introductions was particularly unpleasant: it required searching for references online, adding them to the Zotero library, then moving to the project folder. Most references just migrated from one project to another, but the remaining few were still a pain in the neck.

The solution

Now, I follow the recipe as follows:

  1. Set better bibtex to export my full library to a file in my home, ~/.refs_andrey.bib and keep it updated. Each time I add a new entry to my Zotero library, it ends up exported to this master BIB file.
  2. Add export BIBINPUTS=$HOME to my ~/.zshrc. In fact, the bibliographies are copmpiled by the bibtex executable (or biblatex). We have to make this executable aware that it must look for bib inputs in the folder with the master BIB file. In my case, everything is invoked from the z-shell, if you use a GUI for latex, this step might be more complex. I guess, in the worst case there might be a system’s default location of BIB files (something cryptic along the lines of ../texmf/..).
  3. In the latex source file, we input the BIB file without any paths, as if it is in the same directory with the latex root file, e.g. \bibliography{refs_andrey}.
  4. In the repository with the latex source of the current project, run
jabref -n -a main.aux,refs_andrey.bib ~/refs_andrey.bib
git add refs_andrey.bib && git commit -m "bibtex" && git push
rm ./refs_andrey.bib

This writes all the bibtex entries that were referenced in main.tex to the file refs_andrey.bib in the current directory, commits and pushes it to the repository, and removes.

This way, only the cited references get exposed to the remote repository. Thanks to the BIBINPUTS variable, the project still compiles locally because all the required references (and many more) are in the master bibfile. The project can be compiled at the remote because all the bibtex entries that are cited, have been exported and pushed to the repository. Importantly, since we use the same filename for the master bibfile and the project-local bibfile, we do not have to make any changes to the latex source.