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:

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

ref_in_broken_bib_2

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

ref_before_fix

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:

Screenshot from 2016-01-11 17:22:24

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!:

ref_after_fix

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

ref_in_fixed_bib

*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…

 

17 Comments

  1. As of my version (1.16.2) this does not work anymore, since Mendeley will escape the HTLM tags. However, a little modification to the script fixes the issue.

    #!/usr/bin/python

    # By: Kathy Lam and Jarno
    # Date: August 25, 2016
    # Purpose: Replace all instances of “” with “\textit{”
    # and “
    ” with “}” in bibtex file generated by Mendeley

    oldbib = open(“bibliography.bib”, “r”)
    newbib = open(“new_bibliography.bib”, “w”)
    italics_open = “{\\textless}i{\\textgreater}”
    italics_close = “{\\textless}/i{\\textgreater}”

    for line in oldbib:
    if line.startswith(“title”):
    if italics_open in line:
    fixed_open_tags = line.replace(italics_open, “\\textit{“)
    fixed_both = fixed_open_tags.replace(italics_close, “}”)
    newbib.write(fixed_both)
    else:
    newbib.write(line)
    else:
    newbib.write(line)

    Reply

  2. Hello everyone,

    I found a single line neat way to do it.

    If under “Tools/Options/BibTeX” you have your “Escape LaTeX special characters” checked,
    Under terminal:
    sed -i -e ‘s;{\\textless}i{\\textgreater};\\textit{;g’ -e ‘s;{\\textless}/i{\\textgreater};};g’

    If not checked: (But this causes other problems so you will need to change special characters one by one)
    sed -i -e ‘s;;\\textit{;g’ -e ‘s;;};g’

    Reply

    1. I lost some of the comment because of HTML. 🙂
      If under “Tools/Options/BibTeX” you have your “Escape LaTeX special characters” checked,
      Under terminal:
      sed -i -e ‘s;{\\textless}i{\\textgreater};\\textit{;g’ -e ‘s;{\\textless}/i{\\textgreater};};g’ FILENAME

      If not checked: (But this causes other problems so you will need to change special characters one by one)
      sed -i -e ‘s;(lessthansign)i(greaterthansign);\\textit{;g’ -e ‘s;(lessthansign)/i(greaterthansign);};g’ FILENAME

      Sorry for the previous post

    2. Hi, I am currently writing my bachelor thesis and I stumbled upon this really annoying problem of Mendeley. Anyway, before I start fixing this with the python code I wanted to give it a try with Ozan’s method. This is probably a stupid question but I don’t get where I am suppose to paste the line. In the terminal of my computer (I have a mac BTW)? Or is it also a python code ? Or is it just for Windows ?

      Thank you for your help guys.

    3. Hi Warren. Yes, you need to enter commands in the Terminal, but you have to navigate to the folder that has the file on which you want to operate — look up the “cd” (change directory) command. The “sed” command should work on a Mac, but I have not used Ozan’s solution myself. Good luck!

    4. Thank you for your answer. So after looking up for the sed command in macOS, I was able to modify the command so it’ll work on mac. I don’t know if it’d work on every version (my current version is macOS High Sierra, 10.13.3), but it works! Maybe it can be useful to someone.

      sed -i ” -e ‘s/{\\textless}i{\\textgreater}/\\textit{/g’ -e ‘s/{\\textless}\/i{\\textgreater}/}/g’ FILENAME

  3. Thank you, this is immensely useful! I admit I was surprised that there was no functionality for this in Mendeley, given that, for example, Mendeley can already add journal abbreviations into BibTex. Given that it’s not uncommon for many people to need to use both LaTeX and word processors for different tasks, I would have assumed that needing versatile italics tags would be a (relatively) common use case.

    Reply

  4. Leaving a reply here for what it’s worth: because LaTeX lets you run Terminal commands from within documents, you can do this:

    \immediate\write18{sed -i ” -e ‘s/{\\textless}i{\\textgreater}/\\textit{/g’ -e ‘s/{\\textless}\/i{\\textgreater}/}/g’ /Path/To/File/bibliography.bib}

    and the Terminal command will be run for you whenever you compile. I’m working on a Mac – not sure what version of sed or whatever you’ll need to do on Windows – but finally I’ve got the wretched thing doing what I want, automatically, without me having to remember!

    Reply

    1. Thanks so much for sharing. I hope this is useful to others. Wow, I wrote this blog post in 2016; it’s surprising that this is still an issue!

    2. Yeah, you’d hope that in SIX YEARS this might have been fixed. But hey-ho!

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 )

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