我想要一個命令來提供給定書目條目的作者數量。到目前為止,我未成功的嘗試是建立一個鍵值儲存(prop),掛鉤 bbl 導入機制並儲存應該出現在作者計數器中的作者數量。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
author = {John Doe and Mike Smith},
year = {2001},
title = {Title},
publisher = {Publisher},
}
@book{key2,
author = {John Doe},
year = {2002},
title = {Title2},
publisher = {Publisher2},
}
\end{filecontents}
\usepackage{biblatex}
\addbibresource{\jobname.bib}
\usepackage{expl3}
\ExplSyntaxOn
\prop_new:N \g_my_author_count
\cs_new_protected:Npn \storecount#1#2{%
\prop_gput:Nnn \g_my_author_count{#1}{#2}%
}
\cs_new:Npn \getcount#1{%
\prop_item:Nn \g_my_author_count{#1}%
}
\ExplSyntaxOff
\newcounter{authorcount}
\setcounter{authorcount}{0}
\AtDataInput{%
\storecount{\strfield{entrykey}}{\value{author}}%
% debug stuff begin
\storecount{fakekey}{123}%
\addtocounter{authorcount}{\value{author}}%
\global\edef\entrykey{\strfield{entrykey}}%
% debug stuff end
}
% make sure it's defined
\makeatletter
\@ifundefined{entrykey}{\edef\entrykey{\empty}}{}
\makeatother
\begin{document}
% debug stuff begin
% expect 3
\arabic{authorcount} % got 3 => hook & counter work
% expect key or key2
\entrykey % got key => \strfield{entrykey} works
% expect [?] - 123
[?] - \getcount{fakekey} % got [?] - 123
% => storage works
% debug stuff end
% expect [2] - 2
\cite{key} - \getcount{key} % got [2] -
% expect [1] - 1
\cite{key2} - \getcount{key2} % got [1] -
\printbibliography
\end{document}
我缺什麼?
答案1
author
由於處理條目時資訊存在於計數器中,因此可以使用\cite...
類似命令直接檢索該數字。無需繞道\AtDataInput
。
由於biblatex
的\cite
命令很強大,因此這不會以可擴展的方式返回作者的數量。如果需要的話,確實可能更容易通過\AtDataInput
或讓\cite
類似的命令將相關計數器保存在輔助巨集中。什麼更有意義取決於用例。
\documentclass{article}
\usepackage{biblatex}
\addbibresource{biblatex-examples.bib}
\DeclareCiteCommand{\citeauthorcount}
{\boolfalse{citetracker}%
\boolfalse{pagetracker}%
\usebibmacro{prenote}}
{\printtext{The entry}%
\setunit{\addspace}%
\printfield{entrykey}%
\setunit{\addspace}%
\printtext{has \arabic{author} author(s)}}
{\multicitedelim}
{\usebibmacro{postnote}}
\begin{document}
\citeauthorcount{sigfridsson,worman}
\printbibliography
\end{document}
條目 sigfridsson 有 2 位作者,條目 worman 有 1 位作者
如果我理解正確,您的解決方案的主要問題expl3
是擴展和缺少\the
. MWE 使用n
-type 參數,因此不會儲存計數器的值,而是儲存其內部表示形式,並且與 類似\strfield{entrykey}
:n
版本只會保存\strfield{entrykey}
,但x
版本會擴展它,因此寫入sigfridsson
和worman
。以下實施應該有效。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{biblatex}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g_vh_author_count
\cs_new_protected:Npn \vh_store_author_count:nn #1#2
{
\prop_gput:Nnn \g_vh_author_count {#1} {#2}
}
\cs_generate_variant:Nn \vh_store_author_count:nn {xx}
\cs_new:Npn \vh_get_author_count:n #1
{
\prop_item:Nn \g_vh_author_count {#1}
}
\NewExpandableDocumentCommand \getauthorcount {m} {
\vh_get_author_count:n { #1 }
}
\AtDataInput{%
\vh_store_author_count:xx {\strfield{entrykey}}{\the\value{author}}%
}
\ExplSyntaxOff
\addbibresource{biblatex-examples.bib}
\begin{document}
\cite{sigfridsson} - \getauthorcount{sigfridsson}
\cite{worman} - \getauthorcount{worman}
\edef\foo{\getauthorcount{worman}} \meaning\foo
\edef\foo{\getauthorcount{sigfridsson}} \meaning\foo
\printbibliography
\end{document}
[1] - 2
[2] - 1
宏:->1
宏:->2