I use Mendeley to create my bib-files and then \usepackage[style=authoryear,backend=biber]{biblatex}
to create my citations.
As I have some URLs, I also use DeclareSourcemap
(as suggested in this answer) to replace special characters. This works until I encountered a new case where an ampersand is in the URL.
The entry that creates the issue is (in mybib.bib
):
@article{EP2014,
author = {{European Parliament} and {European Council}},
journal = {Official Journal of the European Union},
number = {L173},
pages = {349--496},
title = {{Directive 2014/65/EU on markets in financial instruments and amending Directive 2002/92/EC and Directive 2011/61/EU}},
url = {https://eur-lex.europa.eu/legal-content/EN/TXT/PDF/?uri=CELEX:32014L0065{\&}from=EN},
volume = {57},
year = {2014}
}
which has a URL containing the escaped ampersand {\&}
which needs to be &
.
I tried using the DeclaresourceMap
like so, but it doesnt work (biber says main.bcf is malformed)
\DeclareSourcemap{
\maps{
\map{ % Replaces '{\&}' with just '&'
\step[fieldsource=url,
match=\regexp{\{\\\&\}},
replace=\regexp{\&}]
}
}
}
When I look in main.bcf
it gets parsed as
<bcf:map_step map_field_source="url" map_match="\{\\\&\}" map_replace="\&"/>
which my text editor marks as an error (due to the ampersand not being properly escaped? thats at least by best guess).
MWE:
main.tex
\documentclass{article}
\usepackage[style=authoryear,backend=biber]{biblatex}
\addbibresource{mybib.bib}
\DeclareSourcemap{
\maps{
\map{ % Replaces '{\&}' with just '&'
\step[fieldsource=url,
match=\regexp{\{\\\&\}},
replace=\regexp{\&}]
}
}
}
\begin{document}
\textcite{EP2014}
\printbibliography
\end{document}
which I then run using either latexmk main
or pdflatex main
and then biber main
.
Any idea how to solve the error and properly escape the ampersand?
Note:
As I use Mendeley for the creation of my bib-file, I cannot change the bib-file directly, and changing the escaping of special characters when exporting from Mendeley messes up other entries, therefore I think I am bound to using DeclareSourcemap
. If you have another option I am open to that as well.