# Automatic substitution of spaces in fields with Biber and Biblatex

I have a pretty large .bib file which fields may contain spaces (e.g. the "series" field can be "SIGMOD '99"). When I print the bibliography, some lines are thus broken on these spaces, which is unpleasing. I'm using XeLateX with Biblatex and Biber.

I would like to automatically replace spaces with non-breaking spaces in those fields. I tried to use DeclareSourcemap, but it didn't work. Here is what I wrote:

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=series,
match={\ }, replace={~}]
}
}
}


This code actually removes the spaces ("SIGMOD '99" appears as "SIGMOD'99"), same with \nobreakspace. Replacing ~ with \, produces something alike to SIGMOD-kern+.1667em elax ’00.

I didn't succeed with renewbibmacro and StrSubstitute either. I used the following renewbibmacro

\renewbibmacro*{series+number}{%
\StrSubstitute{\printfield{series}}{ }{~}%
\printfield{number}%
\newunit}


Any idea?.. I'm also interested in remarks about good practices.

Edit: here is an MWE:

\documentclass[10pt]{article}

\usepackage{fontspec}
\usepackage[backend=biber]{biblatex}

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=series,
match={\ }, replace={\nobreakspace}]
}
}
}

\begin{filecontents}{bib1.bib}
@article{some:article:1964,
author   = "Mister Smart",
title    = {A very difficult narrative of sub-atomic particles in the diary of Louis XIV},
journal  = {Journal for Advanced Thinking},
series = {SIGMOD '99},
}
\end{filecontents}

\begin{document}
Here is an example of text \cite{some:article:1964}.
\printbibliography
\end{document}


When compiling, you can see that the space in the series field disappears, and I want it replaced by a non-breaking space.

• Regarding the good practices: Complete examples instead of code snippets make it much easier to test solutions. – Ulrike Fischer Apr 29 '15 at 10:47

match and replace use regex and LaTeX is seeing the input too. so funny things can happen if you don't escape all the special regex and latex input correctly. Normally it is best to put everything in the \regexp command and then use regex escapes (e.g. \\n as \n is a newline).

You can use this sourcemap:

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=series,
match={\regexp{\s}}, replace={\regexp{\\nobreakspace\x20}}]
}
}
}


This gives this entry in the bbl:

\field{series}{SIGMOD\nobreakspace '99}


The \x20 is there to get a literal space after the command to avoid problems if a letter follows.

An alternative is

 match={\ }, replace={\string\\nobreakspace\string\x20}]


but imho it is much less clearer.

This here is for the tilde:

 match={\regexp{\s}}, replace={\regexp{~}}]


But if you don't know if the tilde is a special char in a regex an additional backslash doesn't harm:

match={\regexp{\s}}, replace={\regexp{\~}}]