
\csxdef
BibLaTeX 데이터베이스의 모든 항목을 반복하고 각 키를 기반으로 생성하고 싶습니다 . 내가 원하는 의사 코드는 다음과 같습니다.
\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}%
예상되는 출력은MWE 이것이 작동하는 경우는 다음과 같습니다.
참고자료:
- 한 가지 옵션은 다음과 같이 Python에서 이 작업을 수행하는 것입니다.bib 데이터베이스를 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
. 모든 항목을 수동으로 인용하거나 사용한 경우 \nocite{*}
일반적으로 큰 차이가 없어야 하지만 항목 복제 또는 상호 참조와 같은 고급 기능을 사용하면 원본에 반영되지 않는 추가 "복제된" 항목이 생길 수 있습니다. 데이터 베이스.
사용 가능한 모든 항목을 반복하는 가장 좋은 방법은 아마도 \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
작동하나요? 우리가 로드하는 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 }
이름이 바뀐 턱받이 파일을 사용하는 테스트 파일 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
이 경우 필드를 인쇄합니다.