2

I would like to have a command that would give me the number of authors of a given bib entry. What I have unsuccessfully attempted so far was to create a key-value storage (prop), hook on the bbl import mechanism and store the number of authors which is supposed to be present in the author counter.

\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}

enter image description here

What am I missing?

2
  • 1
    Welcome to TeX.SX! Sorry, I don't get what does not work as you expected in your code and what is your intended result. – gusbrs Feb 27 '19 at 14:08
  • Thank you! I'm sorry about that. I have put the expected output in the comments inside the code block. I will try to formulate my questions better next time. – Vojtech Hauser Feb 27 '19 at 14:58
4

Since the information is present in the author counter when an entry is processes, the number can be retrieved directly with a \cite...-like command. No detour via \AtDataInput is necessary.

Since the biblatex's \cite commands are robust, this does not give you back the number of authors in an expandable manner. Should that be needed, it might indeed be easier to go through \AtDataInput or let a \cite like command save the relevant counter in a helper macro. What makes more sense will depend on the use case.

\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}

The entry sigfridsson has 2 author(s), The entry worman has 1 author(s)


If I understand correctly, the main issue with your expl3 solution was expansion and a missing \the. The MWE used n-type arguments and so would not store the values of the counter, but rather its internal representation and similarly for \strfield{entrykey}: The n version would just save \strfield{entrykey}, but the x version expands this and so writes sigfridsson and worman. The following implementation should work.

\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

macro:->1

macro:->2

1
  • I needed to get the number in an expandable manner, so the correction to my expl3 attempt solves it for me. Thank you. – Vojtech Hauser Feb 27 '19 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.