2

Is there any way to produce a table that can provide counts based on the @article, @proceedings and other categories in bibtex?

  • 1
    Welcome to TeX.SX! What do you mean by “produce”? Do you want to insert into a LaTeX document a typeset table showing those statistics? Or you just need to compile such statistics for your own use? Please clarify. – GuM Oct 6 '18 at 22:59
  • I want to create a macro that will print a table in the final pdf based on what is in my bib file – Michael Oct 6 '18 at 23:07
2

The number of entries per entry type can easily be counted in biblatex's \AtDataInput hook. The data is then available from the moment the .bbl file is read. If you want to count only one specific \printbibliography the approach could be similar with \AtEveryBibitem.

The code creates a counter for each refsection and type that counts the number of entries of that type in that refsection. A generic counter for each type is defined in terms of the refsection-specific counter for the current refsection.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[style=authoryear, backend=biber]{biblatex}

\usepackage{booktabs}

\addbibresource{biblatex-examples.bib}

\makeatletter
% generic counters: typecounter@article, ...
% will throw an error if Biber hasn't been run yet, so I'd avoid them
\def\do#1{%
  \newcounter{typecounter@#1}%
  \csdef{c@typecounter@#1}{%
    \csname typecounter@\the\c@refsection @#1\endcsname}%
}
\abx@doentrytypes

% initialise refsection-specific counters
\def\init@typecounters#1{%
  \def\do##1{%
    \ifltxcounter{typecounter@#1@##1}
      {}
      {\newcounter{typecounter@#1@##1}}}%
  \abx@doentrytypes}

% count entries of each type for this refsection
% \AtDataInput is executed once for every refsection
\AtDataInput{%
  \ifcsundef{@processed@typecounters@\the\c@refsection}
    {\init@typecounters{\the\c@refsection}%
     \global\cslet{@processed@typecounters@\the\c@refsection}\@empty}
    {}%
  \stepcounter{typecounter@\the\c@refsection @\abx@field@entrytype}}

% produce a table
\newcommand*{\bibtypecounttable}{%
  \let\@bibtypecounttable@collector\@empty
  \def\do##1{%
    \ifltxcounter{typecounter@\the\c@refsection @##1}
      {\ifnum\value{typecounter@\the\c@refsection @##1}>0
         \appto\@bibtypecounttable@collector{%
           \ifcsundef{typecounter@name@##1}
             {##1}
             {\csuse{typecounter@name@##1}}
            & \arabic{typecounter@\the\c@refsection @##1}\\}
       \fi}
      {}}
  \abx@doentrytypes
  \ifblank\@bibtypecounttable@collector
    {} 
    {\begin{tabular}{lc}
       \toprule
        Type & Count \\
        \midrule
        \@bibtypecounttable@collector
        \bottomrule
     \end{tabular}}%
}
\makeatother

% custom names for the table can be defined with
% typecounter@name@<entrytype>
\csdef{typecounter@name@article}{Journal articles}

\begin{document}
\cite{sigfridsson,worman,geer,baez/article,companion}

\printbibliography

\begin{center}
\bibtypecounttable
\end{center}
\end{document}

The MWE shows a few citations together with the bibliography. At the end of the document there is a table listing all entry types with at least one entry and their respective entry counts.

  • Works perfectly and it was exactly what I wanted. One thing to note, I got an error related to the use of biber as a backend but that was quickly fixed using \usepackage[style=authoryear, backend=bibtex]{biblatex} – Michael Oct 10 '18 at 2:00
  • Subsequent question, how can I define custom names for the type @bibtypecounttable variable? For example, instead of article to manually say Journal article. – Michael Oct 10 '18 at 2:07
  • @Michael See the edit. The code show work for both Biber and BibTeX, but you have to run Biber if you have backend=biber in the code: tex.stackexchange.com/q/154751/35864 – moewe Oct 10 '18 at 5:48
  • does the change include how to change the names of the defaults (e.g., article -> journal article). – Michael Oct 11 '18 at 2:47
  • @Michael Yes, see \csdef{typecounter@name@article}{Journal articles}. You can define \csdef{typecounter@name@<entrytype>}{<output>} in the preamble and then <output> will be printed instead of the entry type. I could not be bothered to update the image, but the code as posted now should already produce 'journal article'. – moewe Oct 11 '18 at 5:05

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.