Count number of references using biblatex
You can count references in \AtDataInput
. To avoid specifying the counting/filtering criteria twice, you can create categories.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{csquotes}
\usepackage[backend=biber,style=authoryear]{biblatex}
\DeclareBibliographyCategory{primary}
\DeclareBibliographyCategory{secondary}
\defbibheading{primary}{\subsection*{Primary sources}}
\defbibheading{secondary}{\subsection*{Secondary sources}}
\newcounter{primary}
\newcounter{secondary}
\AtDataInput{%
\ifboolexpr{ test {\ifkeyword{primary}} and not test {\ifentrytype{misc}} }
{\addtocategory{primary}{\thefield{entrykey}}%
\stepcounter{primary}}
{}%
\ifboolexpr{ test {\ifkeyword{secondary}} and not test {\iffieldundef{year}}
and test {\ifnumless{\thefield{year}+0}{1980}} }
{\addtocategory{secondary}{\thefield{entrykey}}%
\stepcounter{secondary}}
{}}
\addbibresource{biblatex-examples.bib}
\begin{document}
In this document, I cite \arabic{primary} primary works and \arabic{secondary}
secondary works.
\nocite{*}
% Filler text \parencite{aristotle:poetics,aristotle:rhetoric,nussbaum,hyman,pines}.
\printbibheading
\bibbycategory
% \bibbycategory is a shorthand for:
% \printbibliography[heading=primary,category=primary]
% \printbibliography[heading=secondary,category=secondary]
\end{document}
This approach also works with BibTeX as the backend. \ifboolexpr
provides a flexible way to combine individual tests. Further details can be found in the biblatex
manual.
A limitation is that \AtDataInput
doesn't distinguish between reference sections. If your document uses these (via the refsection
option or the refsection
environment), you can make the counters section-specific using some commands from etoolbox
.
\newcounter{primary}
\def\theprimary{\csuse{primary:\therefsection}}
\newcounter{secondary}
\def\thesecondary{\csuse{secondary:\therefsection}}
\AtBeginEnvironment{refsection}{%
\csnumgdef{primary:\therefsection}{0}%
\csnumgdef{secondary:\therefsection}{0}}
\AtDataInput{%
\ifboolexpr{ test {\ifkeyword{primary}} and not test {\ifentrytype{misc}} }
{\addtocategory{primary}{\thefield{entrykey}}%
\csnumgdef{primary:\therefsection}{\csuse{primary:\therefsection}+1}}
{}%
\ifboolexpr{ test {\ifkeyword{secondary}} and not test {\iffieldundef{year}}
and test {\ifnumless{\thefield{year}+0}{1980}} }
{\addtocategory{secondary}{\thefield{entrykey}}%
\csnumgdef{secondary:\therefsection}{\csuse{secondary:\therefsection}+1}}
{}}
Here you should only access the counter values with \theprimary
and \thesecondary
.
Ok, so in your preamble, put
\newcounter{refs}
\makeatletter
\defbibenvironment{counter}
{\setcounter{refs}{0}
\renewcommand{\blx@driver}[1]{}
}
{We have \therefs references}
{\stepcounter{refs}}
\makeatother
And use \printbibliography[env=counter,options for selection]
Explanation: you create a new type of bibliography, called "counter". It defined by \defbibenvironment
.
- Type of bibliography, here "counter"
- Secund argument : what is doing at the beginning of bibliography. In your case it's: a. Reset counter b. Say to print nothing when a entry is tested.
- What is printing at the end of bibliography.
- What is printing / executed at each entry.
Sorry for English. If ok, say to me. I will write a french article in my weblog.
I think, you could try this :
\newcounter{reports} \renewbibmacro*{finentry}{\sttepcounter{reports}\finentry} \printbibliography[keyword=MyProject,type=report,title=My project reports] \newcounter{reports2} \renewbibmacro*{finentry}{\stepcounter{reports2}\finentry} \printbibliography[keyword=MyProject,type=report,title=My project reports] This year, My Project produced \thereports reports, while Another Project produced \thereports2 reports.
L.1 and L. 4 : creating counters L.2 and L. 6 : increments counters at each entry of bibliography. L. 7 : prints counter
see also http://geekographie.maieul.net/Une-bibliographie-commentee