# Dealing with italics in bibtex files exported from Mendeley

TL;DR Tags for italicized text in bibtex file generated by Mendeley are not compatible with BibTeX/LaTeX. This is still an issue as of August 2015. I wrote a Python script to fix the tags from <i> and </i> to \textit{}.

I decided to write my Ph.D. thesis in LaTeX because I wanted the culmination of my years of work to beautifully presented. One blip I have encountered though is with my references in the bibtex file generated by the Mendeley reference manager I use. First, let me just say that Mendeley is awesome; I’ve used it for about 6 or 7 years now and has been incredibly useful: I collect, skim, and read a lot of journal articles so it’s been a huge time-saver with its annotation and searching functions. I also have used the plugin for citing in LibreOffice Writer, which has been great (except for this one weird glitch that I think may be related to citing within tables).

So what’s my problem? I needed species and gene names to be italicized in the article titles. This is accomplished by adding <i> and </i> tags in the title field of the Mendeley desktop application, here surrounding the genus name Bacteroides:

But these tags are unchanged in titles of the Mendeley-generated bibtex file:

Which this leads to incorrect characters in the citation of the document:

Am I the first to encounter this problem? It doesn’t seem so. Perhaps there aren’t many Biology graduate students writing their theses in LaTeX. Or perhaps those that do don’t care about proper italicization. I myself am not really a stickler for these details, but some people are and these people may be on the committees that decide the fate of lowly graduate students like me.

I corresponded with the Mendeley Support team on Twitter to see if perhaps they had a fix to this italics tag problem:

The only solution then was to repair the bibtex file myself, which though it is a bit annoying, is actually very straightforward. I wrote the Python script below to change the html-like tags to LaTeX ones*.

For this script to work as is, the following requirements must be met:

• Python version 2.7 needs to be installed (Python 3 may work; haven’t tested)
• The script needs to be in the same directory as the bibtex file it is meant to process.
• The bibtex file must be named “bibliography.bib”; a new file called “new_bibliography.bib” will be generated that has all instances of the <i> and </i> tags replaced with the LaTeX \textit{} command. (Note: be very careful that all instances of <i> and </i> are correct in the Mendeley title field!)

#!/usr/bin/python

# By: Kathy Lam
# Date: January 11, 2016
# Purpose: Replace all instances of "<i>" with "\textit{"
#          and "</i>" with "}" in bibtex file generated by Mendeley

oldbib = open("bibliography.bib", "r")
newbib = open("new_bibliography.bib", "w")

for line in oldbib:
if line.startswith("title"):
if "<i>" in line:
fixed_open_tags = line.replace("<i>", "\\textit{")
fixed_both = fixed_open_tags.replace("</i>", "}")
newbib.write(fixed_both)
else:
newbib.write(line)
else:
newbib.write(line)



In Windows, I think the script can be executed by double-clicking for those who want to avoid using the command-line. On a Mac, the file permissions must be changed to allow it to be executable, which I think has to be done through the command line using chmod +x [script_name.py]; and if you want to execute the script by double-clicking, the extension of the script should be changed from .py to .command.

Here is the reference after the italics have been fixed — huzzah!:

And, for completeness’ sake, here is the corrected bibtex file entry:

*To make the script as universal as possible, I avoided using libraries like BibtexParser. I hope that this script might be useful to someone out there trying to google a solution to this problem…