Citing BibTeX entries generated by Mendeley
15 Aug 2013When Mendeley generates BibTeX files for entries with more than one URL, it concatenates the various URLs in a single BibTeX url field. That’s a problem because it means the URLs will be wrongly formatted when you cite the entry in your LaTeX document.
Mendeley
I use Mendeley intensively for my bibliographic needs. I use it to keep references to all bibliography that I need in my work, I use as a PDF database, I use it to keep my own publications and as my main academic profile on the web, I use it when writing papers on Word or LaTeX. One nice thing about Mendeley, is that is allows you to associate several files and URLs with a single publication. I often take advantage of this in my own publications (see Figure 1) to publish the PDF of a paper, along with any presentations, posters, and other stuff (usually as references to a Figshare item).
BibTeX
Mendeley can generate BibTeX files automatically (it also has plugins for Word and OpenOffice…), however, BibTeX conversion is not very flexible and it has a few problems. One of those problems occurs when entries have more that one URL associated. For example, the entry highlighted in Figure 1, would result in the BibTeX code of Figure 2. Notice that the two URLs have been concatenated in a single ‘url’ field in BibTeX.
If you use that BibTeX code in a LaTeX file the resulting reference will be rendered as Figure 3, which is not what you would want because both URLs were concatenated together.
In my case, what I wanted was to simply render one of the URLs – the one that references the paper itself. My solution was to redefine the \url
command to detect the cases where the passed URL has a space in it, and print only the first URL. For this, I use the xstring LaTeX package:
% I use a custom reference style
\bibliographystyle{myplainnat}
\usepackage{xstring}
% I use a custom reference style
\bibliographystyle{myplainnat}
% Split the URL string using the space character and take only
% the first part.
% Because the entry is formatted by default using the \url command
% we need to first give it another name, and then redefine
% the \url command to our own implementation.
\let\oldurl\url
\renewcommand{\url}[1]{%
\IfSubStr{#1}{ }{%
\StrPosition{#1}{ }[\Result]
\StrLeft{#1}{\Result}[\FirstPart]
\oldurl{\FirstPart}%
}{%
\oldurl{#1}%
}}
\bibliography{PhD}
This will produce something like: