循环遍历整个 Bib 数据库

循环遍历整个 Bib 数据库

我想循环遍历 BibLaTeX 数据库中的所有条目并\csxdef根据每个键生成一个。我想要的伪代码是:

\foreach \EntryKey in \ListOfAllBibliography {%
    \edef\UrlKeyValue{\citefield{\EntryKey}{url}}%
    \StrBehind*{\UrlKeyValue}{//}[\UrlKeyValueExtracted]%
    \IfStrEq{\UrlKeyValue}{}{}{%
        \csxdef{Bib \UrlKeyValueExtracted}{\EntryKey}%
    }%
}%

对于这种特殊情况,此循环应等效于

\csxdef{Bib books.google.com/books?id=hEYuAQAAIAAJ}{knuth1984texbook}%
\csxdef{Bib books.google.com/books?id=54A3MuBzIrEC}{goossens1994latex}%

预期产出平均能量损失 当这个工作时是:

在此处输入图片描述

参考:

代码:

\begin{filecontents}{mybib.bib}
@book{knuth1984texbook,
  title={The texbook},
  author={Knuth, D.E. and Knuth, A.D. and Bibby, D. and American Mathematical Society and Addison-Wesley Publishing Company and Addison-Wesley},
  isbn={9780201134483},
  lccn={85030845},
  series={Computers \& typesetting},
  url={https://books.google.com/books?id=hEYuAQAAIAAJ},
  year={1984},
  publisher={Addison-Wesley},
  myFieldA={Useful Book},
  myFieldB={on Shelf 4},
}
@book{goossens1994latex,
  title={The LaTeX Companion},
  author={Goossens, M. and Mittelbach, F. and Samarin, A.},
  isbn={9780201541991},
  lccn={lc93023150},
  series={Addison-Wesley series on tools and techniques for computer typesetting},
  url={https://books.google.com/books?id=54A3MuBzIrEC},
  year={1994},
  publisher={Addison-Wesley},
  myFieldA={Also Useful Book},
  myFieldB={on Shelf 5},
}

\end{filecontents}

\documentclass{article}
\usepackage{xstring}% used only in pseudo code for this MWE.

\usepackage{pgffor}
\usepackage{etoolbox}

\usepackage{biblatex}
\addbibresource{mybib.bib}

\begin{document}

%% Pseudo code for desired looping:
%    \foreach \EntryKey in \ListOfAllBibliography {%
%        \edef\UrlKeyValue{\citefield{\EntryKey}{url}}%
%        \StrBehind*{\UrlKeyValue}{//}[\UrlKeyValueExtracted]%
%        \IfStrEq{\UrlKeyValue}{}{}{% Skip if the url= key us missing
%            \csxdef{Bib \UrlKeyValueExtracted}{\EntryKey}%
%        }%
%    }%

\foreach \x in {hEYuAQAAIAAJ, 54A3MuBzIrEC} {%
    \ifcsdef{Bib books.google.com/books?id=\x}{%
        PASS: Found (\citefield{\csuse{Bib books.google.com/books?id=\x}}{title}).
    }{%
        FAIL: Missing csdef for \x.
    }%
    \par
}

\end{document}

答案1

从内部biblatex我们永远无法循环遍历.bib数据库中的所有条目。我们只能循环遍历文件中的所有条目.bbl。如果您手动引用所有条目或使用,\nocite{*}通常不会有太大差异,但如果您使用一些更高级的功能,如条目克隆或 crossref,您最终可能会得到原始数据库中未反映的额外“克隆”条目。

循环遍历所有可用条目的最佳方法可能是\AtDataInput。每当从文件读取条目时.bbl(在读入的最后),都会执行此钩子。此时,字段可作为命令序列展开\abx@field@<field name>(因此您不必摆弄\citefield和朋友,因为它们不可扩展,因此在示例中没有任何用处)。

\documentclass{article}
\usepackage{xstring}
\usepackage{pgffor}
\usepackage{biblatex}

\makeatletter
\AtDataInput{%
  \ifundef\abx@field@url
    {}
    {\edef\UrlKeyValue{\abx@field@url}%
     \StrBehind*{\UrlKeyValue}{//}[\UrlKeyValueExtracted]%
     \csxdef{Bib \UrlKeyValueExtracted}{\abx@field@entrykey}}}
\makeatother

\begin{filecontents}{\jobname.bib}
@book{knuth1984texbook,
  title={The texbook},
  author={Knuth, D.E. and Knuth, A.D. and Bibby, D.},
  isbn={9780201134483},
  lccn={85030845},
  series={Computers \& typesetting},
  url={https://books.google.com/books?id=hEYuAQAAIAAJ},
  year={1984},
  publisher={Addison-Wesley},
  myFieldA={Useful Book},
  myFieldB={on Shelf 4},
}
@book{goossens1994latex,
  title={The LaTeX Companion},
  author={Goossens, M. and Mittelbach, F. and Samarin, A.},
  isbn={9780201541991},
  lccn={lc93023150},
  series={Addison-Wesley series on tools and techniques for computer typesetting},
  url={https://books.google.com/books?id=54A3MuBzIrEC},
  year={1994},
  publisher={Addison-Wesley},
  %pages={11-15},
  myFieldA={Also Useful Book},
  myFieldB={on Shelf 5},
}

\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
\nocite{*}

\foreach \x in {hEYuAQAAIAAJ, 54A3MuBzIrEC} {%
    \ifcsdef{Bib books.google.com/books?id=\x}{%
        PASS: Found (\citefield{\csuse{Bib books.google.com/books?id=\x}}{title}).
    }{%
        FAIL: Missing csdef for \x.
    }%
    \par
}
\end{document}

PASS:找到(教科书)。 PASS:找到(LaTeX Companion)。

答案2

您可能知道我的usebib软件包,但我也有一个实验expl3版本。您的问题促使我添加了一项功能,即存储引用键的序列。

它是如何usebib3工作的?对于我们加载的 bib 文件中的每个条目,它都会设置一个包含条目字段的属性列表。然后我们可以通过从属性列表中检索每个字段来访问它。

在您的情况下,您可以创建一个新的属性列表,其中包含字段作为键url以及它们所属的引用键作为值。

usebib3.sty(版本 0.3)

\RequirePackage{expl3,xparse,url}
\ProvidesExplPackage{usebib3}{2022/05/06}{v. 0.3}{Use fields read in bib files}

\prop_new:N \l__usebib_temp_prop
\prop_new:N \g__usebib_temp_prop
\seq_new:N \g_usebib_discard_seq
\seq_new:N \g_usebib_keys_seq

\NewDocumentCommand{\bibinput}{m}
 {
  \clist_map_function:nN { #1 } \usebib_main:n
 }
\NewExpandableDocumentCommand{\usebibentry}{mm}
 {
  \prop_item:cf { g_usebib_entry_#1_prop } { \str_lower_case:n { #2 } }
 }
\NewExpandableDocumentCommand{\usebibentryurl}{O{|}m}
 {
  \__usebib_url:nf { #1 } { \prop_item:cn { g_usebib_entry_#2_prop } { url } }
 }
\NewDocumentCommand{\newbibfield}{m}{}

\@ifpackageloaded{hyperref}
 {
  \cs_new_protected:Nn \__usebib_url:nn
   {
    \url{ #2 }
   }
 }
 {
  \cs_new_protected:Nn \__usebib_url:nn
   {
    \tl_rescan:nn { \url #1 #2 #1 }
   }
 }
\cs_generate_variant:Nn \__usebib_url:nn { nf }
\cs_generate_variant:Nn \prop_item:Nn { cf }

\NewDocumentCommand{\newbibignore}{m}
 {
  \seq_gput_right:Nx \g_usebib_discard_seq { \str_lower_case:n { #1 } }
 }

\cs_new_protected:Nn \usebib_main:n
 {
  \group_begin:
  \char_set_active_eq:NN @ \usebib_read_entry:w
  \char_set_catcode_active:n { `@ }
  \file_input:n { #1.bib }
  \group_end:
 }

\cs_new_protected:Npn \usebib_read_entry:w #1#
 {
  \seq_if_in:NxTF \g_usebib_discard_seq { \str_lower_case:n { #1 } }
   {
    \use_none:n
   }
   {
    \__usebib_read_entry:w
   }
 }

\cs_new_protected:Npn \__usebib_read_entry:w
 {
  \group_begin:
  \char_set_catcode_other:n { `@ }  % may appear in text
  \char_set_catcode_other:n { `\% } % can be used in URLs
  \char_set_catcode_other:n { `\# } % can be used in fields
  \__usebib_read_entry:n
 }

\cs_new_protected:Nn \__usebib_read_entry:n
 {
  \prop_gset_from_keyval:Nn \g__usebib_temp_prop { USEBIBREADKEY=#1 }
  \group_end:
  \__usebib_save_entry:
 }

\cs_new_protected:Nn \__usebib_save_entry:
 {
  \prop_clear:N \l__usebib_temp_prop
  \prop_map_inline:Nn \g__usebib_temp_prop
   {
    \str_if_eq:nnTF { ##1 } { USEBIBREADKEY }
     {
      \seq_gput_right:Nn \g_usebib_keys_seq { ##2 }
     }
     {
      \prop_put:Nxn \l__usebib_temp_prop { \str_lower_case:n { ##1 } } { ##2 }
     }
   }
  \prop_new:c { g_usebib_entry_ \prop_item:Nn \g__usebib_temp_prop { USEBIBREADKEY } _prop }
  \prop_gset_eq:cN
   { g_usebib_entry_ \prop_item:Nn \g__usebib_temp_prop { USEBIBREADKEY } _prop }
   \l__usebib_temp_prop
 }
\cs_generate_variant:Nn \prop_put:Nnn { Nx }

使用重命名的 bib 文件的测试文件petergrill.bib(此处未显示)

\documentclass{article}
\usepackage{usebib3}

\bibinput{petergrill}

\ExplSyntaxOn

\prop_new:N \l_petergrill_url_prop

%% save the url fields
\seq_map_inline:Nn \g_usebib_keys_seq
 {
  \prop_put:Nxn \l_petergrill_url_prop { \usebibentry { #1 } { url } } { #1 }
 }

\NewDocumentCommand{\checkids}{mm}
 {% #1 = prefix, #2 = list
  \clist_map_inline:nn { #2 }
   {
    \prop_if_in:NnTF \l_petergrill_url_prop { #1##1 }
     {
      PASS:~Found~(\usebibentry{\prop_item:Nn \l_petergrill_url_prop { #1##1 }}{title}) \par
     }
     {
      FAIL:~Missing~##1
     }
   }
 }

\ExplSyntaxOff

\begin{document}

\checkids{https://books.google.com/books?id=}{hEYuAQAAIAAJ, 54A3MuBzIrEC}

\end{document}

\seq_map_inline:Nn部分填充了开头提到的属性列表并\checkids查看项目是否存在,title在本例中打印该字段。

在此处输入图片描述

相关内容