From Mendeley to LaTeX: a seamless integration

December 10, 2016

Mendeley is the only reference manager, as I know, capable of updating a bib file on the fly. That is: as soon as you import a new reference or make a change to an existing reference, it will export the updated version of the library to the library.bib file in a predefined folder. This is especially nice if you’re writing a paper and constantly searching for new literature. If you’re only bothering with writing one paper, then this might be enough. When you write the second paper, simply instruct Mendeley to write to a different file. However, if you’re writing two or more scientific documents using the same set of references, then you will need something smarter. What’s more problematic is that Mendeley dumps everything inside the references, including location of the PDF and any notes you make. This might cause LaTeX to fail to compile.

The seamless framework explained

Let’s say Mendeley is set to update /path/to/tex-projects/library.bib, where /path/to/tex is the location of the parent folder of all your LaTeX documents, and library.bib is the default name for the bib file (you can’t change it). Let’s also say you are working on 3 projects paper, report and book which uses the same reference set. Of course you can just use the absolute path to library.bib in all these projects - but this makes your project non-portable. So you will need to use separate bib files (with the same content) in all projects. What we need is a tool that monitors changes to library.bib, which on change detected, will do the following:

  1. Read library.bib and clean up all the unnecessary fields.
  2. Copy library.bib to /path/to/tex-projects/paper/, /path/to/tex-projects/report/ and /path/to/tex-projects/book/

So, let’s do it.

Configure Mendeley.

This step is simple: go to Mendeley’s Preferences (keyboard shortcut +,), go to tab BibTeX and select the following options:

Clean up library.bib

I have written a small Python code that does this automatically. You can download it here. Let’s say you save it to /path/to/tex-projects/. I currently hardcode it to remove annote, annotate, abstract, mendeley-groups, keywords, file. These fields are quite problematic because they might be long and contain special character. I also make it comment out issn, isbn, doi, just in case I want to have a quick look. All the other fields are kept intact. The utility takes two arguments, as following:


cleanbib.py --file /path/to/tex-projects/library.bib --out /path/to/tex-projects/cleaned.bib

This tells python to read the original references in library.bib, clean it up and save the result in cleaned.bib

Copy to multiple places

To make it simple, I wrote a small script to do this, after calling cleanbib, it also omits a notification on screen (only works for Mac). The script is pretty simple:


#!/bin/bash

# Omit a message
osascript -e 'display notification "library.bib cleaned up" with title "Mendeley change detected"'

# Call the python script
python /path/to/tex-projects/cleanbib.py -f /path/to/tex-projects/library.bib -o /path/to/tex-projects/cleaned.bib

# Copy the result to all projects:
cp /path/to/tex-projects/cleaned.bib /path/to/tex-projects/paper/cleaned.bib
cp /path/to/tex-projects/cleaned.bib /path/to/tex-projects/report/cleaned.bib
cp /path/to/tex-projects/cleaned.bib /path/to/tex-projects/book/cleaned.bib

Let’s say you save this bash script in /path/to/tex-projects/cleanbib.sh

Putting it all together

The last thing we need is to listen to changes in library.bib and run the bash code on change detected. fswatch is a nice cross-platform tool that provides the functionality we want. We will hook fswatch with cleanbib.sh with the following command:


fswatch -o /path/to/tex-projects/library.bib | xargs -n1 /path/to/tex-projects/cleanbib.sh &

That’s it. Now fswatch will work behind the scene, detecting any changes that Mendeley makes to library.bib, then call cleanbib.sh to clean up the bib file and deploy it to all the projects that you’re working on. With this, as soon as you make any changes in Mendeley, you can immediately go back to your TeX editor (such as TexStudio - my recommendation) and cite the new or changed reference.

Future work

Obviously, these are quite some steps to do just a simple thing. In theory, a single Python file should be able to do all that - listen to file change, displaying notification, clean up the bib file and deploy to several places. In addition, it should be possible for user to provide a set of key they want to keep as argument, instead of having them hardcoded. This is dedicated as future work.


If you like this post, please consider sharing with , , , , or leave me a comment below.


comments powered by Disqus