\DeclareSourceMap 不起作用

\DeclareSourceMap 不起作用

我有以下 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、、编译了该文档,但收到警告:biberxelatexxelatex

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

这里有两个问题。

  1. 除非明确指定,否则不会覆盖非空字段overwrite。(空字段也可以,但这很快就会派上用场。)
  2. 您可能不想覆盖现有的关键字,而是想附加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]
    }
  }
}

相关内容