2

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.

2 Answers 2

3

The .bcf file that biblatex uses to communicate the sourcemaps to Biber is essentially an XML file. As far as I can see XML files don't really like &s (https://www.w3.org/TR/xml/#syntax), so we can replace & with its Unicode code point \x{26}

\documentclass{article}

\usepackage[style=authoryear,backend=biber]{biblatex}

\DeclareSourcemap{
  \maps{
    \map{ % Replaces '{\&}' with just '&'
      \step[fieldsource=url,
        match=\regexp{\{\\\x{26}\}},
        replace=\regexp{\x{26}}]
    }
  }
}

\usepackage{filecontents}
\begin{filecontents}{\jobname.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}
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
\textcite{EP2014}
\printbibliography
\end{document}

URL: https://eur-lex.europa.eu/legal-content/EN/TXT/PDF/?uri=CELEX:32014L0065&from=EN.

But really you should complain to the Mendeley people, the URL field is a verbatim field where characters should not be escaped at all.

2
  • Wonderful, that does it! I know that Mendeley is -ehm- suboptimal and doesnt really work well with *TeX. But I got used to being able to search through all pdfs (not supported by zotero afaik) and open them in-app. If you have a better alternative (open-source?), I am all ears! Thanks again for the solution.
    – David
    Commented Aug 2, 2018 at 11:02
  • 1
    @David Sorry, I don't know about a good alternative. I would have suggested Zotero (where Better BibTeX can help you obtain proper .bib files without weird quirks), but there are also more basic .bib file tools like JabRef, BibDesk (Mac only). I don't use any of these tools, so all I can tell you is that from time to time problems with Mendeley pop up here and there. Even if you found a work-around, you should still contact the developers and complain. I have no high hopes that they will do anything about it, but they should know...
    – moewe
    Commented Aug 2, 2018 at 11:14
1

When using a XML configuration file for the biber tool, the answer provided by moewe can be implemented by:

<config>
  <sourcemap>
        <!-- Replace {\&} with & in URL field -->
        <map>
          <map_step map_field_source="url" map_match="\{\\\x{26}\}" map_replace="\x{26}"/>
        </map>
    </maps>
  </sourcemap>
</config>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .