[Tex/LaTex] How to replace a given string in a bibliography with biblatex

biblatexemphasisstrings

Mendeley allows you to emphasise Latin names in article or report titles using HTML notation like so:

A treatise on the ecology of <I>Rattus rattus</I>

Using biblatex, the tags are passed on "verbatim" and of course LaTeX doesn't understand them. I would like to replace all <I> with \emph{ and all </I> with }.

I have tried using \DeclareSourceMap as follows:

\DeclareSourcemap{ 
    \maps[datatype=bibtex]{
      \map{
           \step[fieldsource=title, match={<I>}, replace={\emph{}]
            \step[fieldsource=title,match={</I>},replace={}}]
          }
    }      
}

But of course LaTeX chokes as the extra { and } confuse it.

How should I escape the braces for this to work?

Best Answer

I found that the problem was more with getting Biber to accept what look like XML tags than it was with dealing with the \emph, which can be done by wrapping them in a \regexp, with escapes. In the end I resorted to specifying the < and > by character number.

Anyway, this seems to work.

\DeclareSourcemap{ 
    \maps[datatype=bibtex]{
      \map{
           \step[fieldsource=title,
                 match=\regexp{\x3cI\x3e},
                 replace=\regexp{\\emph\{}]
           \step[fieldsource=title,
                 match=\regexp{\x3c/I\x3e},
                 replace=\regexp{\}}]
          }
    }      
}

Of course, as any fule kno, parsing HTML with regular expressions is not guaranteed to be robust.

Full MWE

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{ratty,
  author = {Mouse, Michael},
  title  = {A Treatise on <I>rattus rattus</I>},
  publisher = {Rodent Publishers},
  date = {2012},
}
\end{filecontents}
\usepackage[style=authoryear,backend=biber]{biblatex}
\DeclareSourcemap{ 
    \maps[datatype=bibtex]{
      \map{
           \step[fieldsource=title, match=\regexp{\x3cI\x3e}, replace=\regexp{\\emph\{}]
           \step[fieldsource=title, match=\regexp{\x3c/I\x3e}, replace=\regexp{\}}]
          }
    }      
}
\addbibresource{\jobname.bib}
\begin{document}

\nocite{*}

\printbibliography

\end{document}

enter image description here