Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am creating an annotated bibliography. Is there a way of obtaining a count of the number of references in a bibliography meeting certain criteria and inserting that into a document?

For example using biblatex I create one section using:

\nocite{*} ;; include all references in .bib file
\printbibliography[keyword=MyProject,type=report,title=My project reports]

and another using:

\printbibliography[keyword=AnotherProject,type=report,title=Another project's reports]

Somewhere else in the document I would like to be able to put a table with counts of number of number of reports by project, or include these counts in text. So for example:

"This year, My Project produced XX reports, while Another Project produced YY reports."

Any ideas on how to get started on that?

Update:

Based on Maieul's excellent and quick answer, I have the following MWE:

\documentclass[oneside,oldfontcommands,9pt,a4paper]{memoir}
\usepackage{url}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[style=authoryear,natbib=true,backend=biber,url=false,doi=false,eprint=false,isbn=false,maxbibnames=98]{biblatex}
\addbibresource{mybiblio.bib}
\begin{document}
\nocite{*}
\chapter{Introduction}
My project produced \thearticles peer-reviewed articles and Another Project produced \theotherarticles peer-reviewed articles. \par 
Note - no numbers here.
\newcounter{articles}
\renewbibmacro*{finentry}{\stepcounter{articles}\finentry}

\printbibliography[keyword=MyProject,title=My project peer-reviewed articles,type=article]
\newcounter{otherarticles}
\renewbibmacro*{finentry}{\stepcounter{otherarticles}\finentry}
\printbibliography[keyword=AnotherProject,title=Another project peer-reviewed articles,type=article]
My project produced \thearticles peer-reviewed articles and Another Project produced \theotherarticles peer-reviewed articles. \par

This works

\end{document}

Where mybiblio.bib looks like:

@ARTICLE{Other2000, 
author = { Other, A N},
title = { my article},
journal = { Some journal },
keywords = {MyProject},
year = {2000},
number={2},
volume = {343},
pages = {230-8}
}
@article{Author1993,
author = {Author, E and Author, M},
journal = {Some Journal},
keywords = {MyProject},
number = {4},
pages = {424--428},
title = {{Another article}},
volume = {6},
year = {1993}
}
@article{Author1997a,
author = {Author, F and Author, T },
journal = {Another journal},
keywords = {AnotherProject},
title = {{A third article}},
year = {1997}
}

However, as you will see I cannot refer to the counters earlier in the document than the \printbibliography commands. Is there any way to be able to refer to the counters early on?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Ok, so in your preamble, put

\newcounter{refs}
\makeatletter
\defbibenvironment{counter}
  {\setcounter{refs}{0}
  \renewcommand{\blx@driver}[1]{}
  }
  {Nous avons \therefs}
  {\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.

  1. Type of bibliography, here "counter"
  2. Secund argument : what is doing at the beginining of bibliography. In your case it's : a. Reset counter b. Say to print nothing when a entry is tested.
  3. What is printing at the end of bibliography.
  4. What is printing / executed at each entry.

Sorry for English. If ok, say to me. I will write a french article in my weblog.

share|improve this answer
    
And you also must write \defbibheading{counter}{} to have no heading in \printbibliography[heading=counter,env=counter,options for selection]. –  Maïeul Aug 12 '12 at 16:23
    
This works thanks - does what I need. Does take a LONG time to compile though once you have several combinations of categories and a big bibliography. –  user8786 Aug 12 '12 at 21:04
    
ok, cool. I publish in my weblog geekographie.maieul.net/… –  Maïeul Aug 12 '12 at 21:24
add comment

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.

share|improve this answer
    
This looks interesting. Does it work with \nocite{*} or does it depend on explicit citation of each item in the bibliography? Can it be generalised to multiple criteria/keywords (eg type=report AND keyword=MyProject AND notkeyword=ProgressReport)? Thanks. –  user8786 Aug 12 '12 at 21:06
    
oh, it's a better way ! no problem of time for running. It's working with nocite. And you can make a lot of test : for exemple :\ifkeyword{toto}{\ifkeyword{titi}{}{\stepcounter{toto}}}{}. You should read the manual of biblatex : theres is a lot of test. –  Maïeul Aug 12 '12 at 21:30
    
but the manual say Note that, if there are any refsection environments in the document and an entry is cited in more than one of them, the ⟨code⟩ may be executed multiple times for an entry. I don't know in your case. But why not. –  Maïeul Aug 12 '12 at 21:35
    
@Audrey yes, that is why it could be a problem, in some context. But I think you suggestion is better than mine, for time processing. –  Maïeul Aug 12 '12 at 22:18
    
This works well, and is fast. However I prefer Maieul's method since it uses the same selection criteria as \printbibligraphy. This allows me to set say \myprojectreports to "keyword=myproject,type=report" once and refer to it when counting and when generating the bibliography. Some of my selection criteria are quite complicated, so keeping a single copy is much easier. Thanks though - I'm sure this will come in useful someday. –  user8786 Aug 13 '12 at 12:11
show 1 more comment

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

share|improve this answer
    
Merci infiniment. Works very well. One question though - with this I can refer to the counter (\thereports) after the \printbibliography command, but not in a preceeding chapter or page. Is there anyway to set the scope of the counter so I can refer to it earlier on in the document. Or is there a "\printphantombibliography" command I can use early on to count but not print the number of citations? –  user8786 Aug 12 '12 at 11:05
    
sorry, I don't understand what do you want to do. Could you reformulate ? Maybe you could try in french ? –  Maïeul Aug 12 '12 at 11:21
    
Salut. J'ai ajouté un MWE qui explique un peu. En fait la bibliographie aura plusieurs chapitres - un par projet - et je souhaite avoir au début du doc un chaptire de synthèse qui va presenter le nombre d'articles publié par chaque projet, le nombre de rapports techniques etc. –  user8786 Aug 12 '12 at 13:21
    
@user8786: Just move the \newcounter{...} lines to the preamble. As usual in such situation, you need to compile twice to get correct values. –  Daniel Aug 12 '12 at 13:58
    
@Daniel: That gives me zeros in the main text, and the correct numbers in the bibliography, presumably since the value really is zero at that stage. I guess I somehow need to stop latex expanding the counters until after the bibliography has been printed? –  user8786 Aug 12 '12 at 14:11
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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