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…
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)
Thanks for the fix!
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
Thanks for taking the time to comment; hopefully it will be useful to others! 🙂
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.
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!
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
Glad to hear you got it to work, and thanks for sharing your solution.
Good luck with your Bachelor’s thesis!
[…] Fortunately, some little programming can help here. The following code, provided by Kathy Lam (see here), opens the exported *.bib file and searches for the italics HTML code and replaces it with the […]
[…] desktop (as md onwards) does not support latex/bibtex. This issue has been already addressed by Kathy Lam); her solution to this issue was implemented in python in order to replace automatically the […]
[…] in typesetting, and the satisfaction of a beautifully rendered document. I’ve actually written about LaTeX before (my most viewed post, actually!), where I discussed a solution to an italicization issue […]
[…] a species name should be in italics in the title. I solved this by downloading the .bib file, run this helpful python script and reupload my new .bib file. That worked like a charm! Nevertheless, manual curation of the […]
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.
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!
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!
Yeah, you’d hope that in SIX YEARS this might have been fixed. But hey-ho!