Italic species names in Mendeley exported *.bib files

I stumbled upon this annoying problem with my reference manager Mendeley: while it is fully capable of italicising journal names where necessary, it lacks the ability to italicise species names.

Screen Shot 2017-08-09 at 07.24.18.png

An easy solution is to simply use the HTML code for italic font:

Screen Shot 2017-08-09 at 07.26.18.png

And there you go, now the species name will be italicised when you let Mendeley automatically generate a reference list in, say, MS Word. However, in case you export your references as *.bib file to use it in LaTeX you will find that the HTML code was not translated correctly and thus your References will look like this:

Screen Shot 2017-08-09 at 07.58.03.png

Well, that isn’t useful at all. Fortunately, some little programming can help here. The following code, provided by Kathy Lam (see here), opens the exported *.bib file,  searches for the italics HTML code and replaces it with the correct LaTeX code.

If you want to run the code on your Mac, simply copy the following code to a text file:


#!/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)

Replace bibliography.bib with the name of your *.bib file. Save this text file as a *.py file. Then, to make it executable, change over to the Terminal and type chmod +x FILE.py, where FILE is the name of the text file you just created. Finally, put the python file and your bib file in the same directory and then double click the python file. All reference should be fixed now. Let’s see:

Screen Shot 2017-08-09 at 08.13.23.png

Thanks Kathy! And good luck everyone with fixing your references.

By the way, there is an even faster way, see Ozan’s comment.

Advertisements

One thought on “Italic species names in Mendeley exported *.bib files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s