我有以下 MWE:
\documentclass{article}
\usepackage{polyglossia}
\setmainlanguage{english}
% Bibliography
\usepackage[backend=biber]{biblatex}
\addbibresource{custom.bib}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue=hit]
}
}
}
\begin{document}
\printbibliography[keyword=hit]
\end{document}
据我了解,它应该将关键字添加hit
到名为“Muster”的作者的所有条目中,然后可以打印\printbibliography[keyword=hit]
(正如我在这里找到的那样:https://tex.stackexchange.com/a/65145/11820)
我使用xelatex
、、编译了该文档,但收到警告:biber
xelatex
xelatex
Package biblatex Warning: Keyword 'hit' not found on input line 21.
并且参考书目中没有印刷任何内容。
还有其他特定的\DeclareSourcemap
工作先决条件吗,或者我的理解完全错误?
该custom.bib
文件内容如下:
@book{key,
author = {Max Muster},
maintitle = {Der Werktitel},
title = {Titel des zweiten Bandes},
volume = {2},
location = {Ort},
year = {2002},
keywords = {muster, etwas}
}
@book{other,
author = {Michael Karomann},
maintitle = {Etwas},
title = {Nichts},
volume = {3},
location = {Ort},
year = {2002},
keywords = {test, etwas}
}
答案1
这里有两个问题。
- 除非明确指定,否则不会覆盖非空字段
overwrite
。(空字段也可以,但这很快就会派上用场。) - 您可能不想覆盖现有的关键字,而是想附加
hit
到关键字。如果这样做,您还需要添加逗号。
要捕获未定义和非空关键字字段的情况,请使用
\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}]
}
}
}
第一个\map
仅匹配非空keywords
字段并添加,hit
,因此命中不会融合到最后一个关键字中keywords = {muster, etwashit}
。第二个\map
不会覆盖现有字段,因此仅在未定义keyword
字段时才适用。它只是使keywords
字段读取hit
。
MWE(此 MWE 使用filecontents
。因此,它会在没有事先警告.bib
的情况下覆盖与同名的现有文件.tex
。为了安全起见,请在空文件夹中进行测试。)
\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}
自然
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
}
}
本来可以更短,但如果.bbl
字段keywords
为空(\keyw{,hit}
),则会留下“空关键字”。这不会造成问题,但我认为这是错误的格式。
如果你是那些习惯于将字段留空的人之一keywords = {}
,那么你需要进一步\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}]
}
}
}
答案2
这是对moewe 的回答,只是提供一种添加关键字而不覆盖现有关键字的替代方法。方法是将关键字附加,keyword
到所有感兴趣的条目(无论它们是否为空),然后根据情况删除最终的前导逗号。
\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={}]
}
}
}
完整的 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}
更新:还有一种方法。如果关键字字段不为空,则添加逗号。然后添加keyword
。(我试图将其放入单个地图中,但做不到,也许有办法)。
\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]
}
}
}
或者更短的(作者 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]
}
}
}