\DeclareSourceMap does not work
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}]
}
}
}
This is a complement to moewe's answer, just providing an alternative method to add a keyword without overwriting existing ones. It is done by appending ,keyword
to all entries of interest, irrespective of them being empty or not, and then removing and eventual leading comma, as the case may be.
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
\step[fieldsource=keywords, match=\regexp{\A,}, replace={}]
}
}
}
A full MWE:
\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[fieldset=keywords, fieldvalue={,hit}, append]
\step[fieldsource=keywords, match=\regexp{\A,}, replace={}]
}
}
}
\begin{document}
\nocite{*}
\printbibliography[keyword=hit]
\end{document}
Update: Still another method. If keyword field is not empty, append a comma. Then append keyword
. (I tried to fit it in a single map, but couldn't, perhaps there's a way).
\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={,}, append]
}
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}, append]
}
}
}
Or shorter (by moewe):
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A(.+)\Z}, replace=\regexp{$1,}] %$
\step[fieldset=keywords, fieldvalue={hit}, append]
}
}
}