参考文献の著者を数える

参考文献の著者を数える

特定の bib エントリの著者数を取得するコマンドが必要です。これまで私が試みたが失敗したのは、キー値ストレージ (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...-like コマンドを使用して数値を直接取得できます。経由の迂回は\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型の引数を使用したため、カウンターの値ではなく、その内部表現 が格納され、 についても同様です\strfield{entrykey}nバージョンは を保存するだけです\strfield{entrykey}が、xバージョンはこれを展開するため、 と を書き込みますsigfridssonworman次の実装は機能するはずです。

\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

関連情報