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…

 

Advertisements

7 thoughts on “Dealing with italics in bibtex files exported from Mendeley

  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)

  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’

    • 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

  3. Pingback: Italic species names in Mendeley exported *.bib files – infinite degrees of freedom

  4. Pingback: Bibtex and Mendeley-Desktop – Gustavo A. Ballen

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 )

Google+ photo

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

Connecting to %s