我想为我的 bibfile 创建某种索引,除以 bibfile 中的每个关键字。我可以使用
\nocite(*)
\printbibliography[keyword=SomeKeyword]
并手动对每个关键字重复此操作。但我想要一个动态解决方案。是否有可能以某种方式循环遍历 bibfile 中的每个关键字?包含多个关键字的条目会出现多次是理想的行为。
答案1
我们可以\blx@addkeyword
收集已知关键字列表,然后循环遍历这些关键字。
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[backend=biber, style=authoryear]{biblatex}
\makeatletter
\def\blx@addkeyword#1#2{%
\xifinlistcs{\detokenize{#2}}{blx@keywords@\the\c@refsection}
{}
{\listcsxadd{blx@keywords@\the\c@refsection}{\detokenize{#2}}}%
\listcsxadd{blx@keyw@\the\c@refsection @\detokenize{#2}}{#1}}
\newcommand*{\DefineKeywordTitle}[2]{%
\csdef{keyword@title@#1}{#2}}
\def\blx@bibbykeyword@i#1{%
\ifcsundef{keyword@title@#1}
{\printbibliography[keyword=#1, title=#1]}
{\printbibliography[keyword=#1, title=\csuse{keyword@title@#1}]}}
\newcommand*{\bibbykeyword}{%
\ifcsundef{blx@keywords@\the\c@refsection}
{}
{\forlistcsloop{\blx@bibbykeyword@i}{blx@keywords@\the\c@refsection}}}
\makeatother
\DefineKeywordTitle{dinosaur}{Works about dinosaurs}
\begin{filecontents}{\jobname.bib}
@book{elk,
author = {Anne Elk},
title = {A Theory on Brontosauruses},
year = {1972},
publisher = {Monthy \& Co.},
location = {London},
keywords = {dinosaur},
}
@book{nussbaum,
author = {Nussbaum, Martha},
title = {Aristotle's \mkbibquote{De Motu Animalium}},
date = 1978,
publisher = {Princeton University Press},
location = {Princeton},
keywords = {secondary},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{*}
\bibbykeyword
\end{document}
如果你需要对关键字进行排序,你可以尝试类似
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[backend=biber, style=authoryear]{biblatex}
\ExplSyntaxOn
\makeatletter
\def\blx@addkeyword#1#2{%
\seq_if_exist:cF {g_hchr_keyword_\the\c@refsection _seq}
{
\seq_new:c {g_hchr_keyword_\the\c@refsection _seq}
}
\seq_if_in:cxF {g_hchr_keyword_\the\c@refsection _seq} {\detokenize{#2}}
{
\seq_gput_left:cx {g_hchr_keyword_\the\c@refsection _seq} {\detokenize{#2}}
}
\listcsxadd{blx@keyw@\the\c@refsection @\detokenize{#2}}{#1}}
\newcommand*{\DefineKeywordTitle}[2]{%
\csdef{keyword@title@\detokenize{#1}}{#2}}
\def\blx@bibbykeyword@i#1{%
\ifcsundef{keyword@title@\detokenize{#1}}
{\printbibliography[keyword=#1, title=#1]}
{\printbibliography[keyword=#1, title=\csuse{keyword@title@\detokenize{#1}}]}}
\newcommand*{\bibbykeyword}
{
\seq_if_exist:cT {g_hchr_keyword_\the\c@refsection _seq}
{
\seq_sort:cn {g_hchr_keyword_\the\c@refsection _seq}
{
\str_compare:nNnTF { ##1 } > { ##2 }
{ \sort_return_swapped: }
{ \sort_return_same: }
}
\seq_map_function:cN {g_hchr_keyword_\the\c@refsection _seq} \blx@bibbykeyword@i
}
}
\makeatother
\ExplSyntaxOff
\DefineKeywordTitle{dinosaur}{Works about dinosaurs}
\begin{filecontents}{\jobname.bib}
@book{elk,
author = {Anne Elk},
title = {A Theory on Brontosauruses},
year = {1972},
publisher = {Monthy \& Co.},
location = {London},
keywords = {dinosaur},
}
@book{nussbaum,
author = {Nussbaum, Martha},
title = {Aristotle's \mkbibquote{De Motu Animalium}},
date = 1978,
publisher = {Princeton University Press},
location = {Princeton},
keywords = {secondary},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{*}
\bibbykeyword
\end{document}
尽管我们实际上不应该这样做,但我们过去常常\str_compare:nNnTF
对字符串进行排序。interface3