There are two issues here.
- Non-empty fields are not overwritten unless
overwrite
is explicitly specified. (Empty fields are fine, though, this'll come in handy soon.)
- You probably do not want to overwrite the existing keywords, instead you want to append
hit
to the keywords. If you do that, you also need to add a comma.
To catch both the cases with an undefined and non-empty keyword field, use
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A.+\Z}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
}
}
The first \map
only matches non-empty keywords
field and adds ,hit
so hit does not meld into the last keyword as keywords = {muster, etwashit}
. The second \map
does not overwrite existing fields, so it only applies if no keyword
field is defined. It simply makes the keywords
field read hit
.
MWE (This MWE uses filecontents
. It therefore overwrites an existing .bib
file with the same name as the .tex
without advance warning. To be on the safe side test this in an empty folder.)
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
author = {Max Muster},
maintitle = {Der Werktitel},
title = {Titel des zweiten Bandes},
volume = {2},
location = {Ort},
year = {2002},
keywords = {muster, etwas},
}
@book{keya,
author = {Max Muster},
maintitle = {Der Werktitel},
title = {Titel des Dritten Bandes},
volume = {3},
location = {Ort},
year = {2002},
}
@book{other,
author = {Michael Karomann},
maintitle = {Etwas},
title = {Nichts},
volume = {3},
location = {Ort},
year = {2002},
keywords = {test, etwas},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A.+\Z}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
}
}
\begin{document}
\nocite{*}
\printbibliography[keyword=hit]
\end{document}

Naturally
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
}
}
would have been shorter, but that leaves an "empty keyword" in the .bbl
if the keywords
field was empty (\keyw{,hit}
). This should not cause problems, but I consider this bad form.
If you are one of those people who are into the perverse habit of leaving fields empty as in keywords = {}
, you need one further \map
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A.+\Z}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, notmatch=\regexp{.+}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
}
}