
我想循環遍歷 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}%
的預期輸出微量元素 當這有效時:
參考:
- 一種選擇是按照 python 執行此操作將書目資料庫轉換為 csv,但更喜歡用 LaTeX 來做。
代碼:
\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
文件中的所有條目。 If you manually cited all entries or used \nocite{*}
there should usually not be that much of a difference, but if you use some more advanced feature like entry cloning or crossref, you may end up with additional "cloned" entries that are not reflected in the original資料庫.
循環所有可用條目的最佳方法可能是\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}
答案2
你可能知道我的包,但我也有它的usebib
實驗版本。expl3
你的問題促使我添加一個功能,即儲存引文鍵的序列。
怎麼usebib3
運作?對於書目文件中的每個條目,我們載入它並設定一個包含條目欄位的屬性清單。然後我們可以透過從屬性清單中檢索每個欄位來存取它。
在您的情況下,您可以建立一個新的屬性列表,其中包含欄位作為鍵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
在本例中列印該欄位。