# Count the number of entries inside each refsection in biblatex

I am formatting my publications in LaTeX. Below is the content of the main file:

\documentclass{article}
\usepackage[style=alphabetic]{biblatex}

\begin{document}
\section{Publications}
\subsection{Journal Articles (1)}
\begin{refsection}
\nocite{journal1}
\end{refsection}

\subsection{Conference Articles (3)}
\begin{refsection}
\nocite{conference1}
\nocite{conference2}
\nocite{conference3}
\end{refsection}
\end{document}


Below is the sample of bibliography:

@article{journal1,
author  = {Author 1},
title   = {Title 1},
journal = {Journal 1},
year    = 1993,
number  = 2,
pages   = {201-213},
month   = 7,
volume  = 4
}
@inproceedings{conference1,
title        = {Title 1},
year         = 1993,
author       = {Author 1},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 1},
publisher    = {Publisher 1},
}
@inproceedings{conference2,
title        = {Title 2},
year         = 1993,
author       = {Author 2},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 2},
publisher    = {Publisher 2},
}
@inproceedings{conference3,
title        = {Title 3},
year         = 1993,
author       = {Author 3},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 3},
publisher    = {Publisher 3},
}


This code generates the following PDF:

Please notice, I have manually mentioned \subsection{Journal Articles (1)} and \subsection{Conference Articles (3)}.

What is a good way to count the number of entries inside each refsection?

You can count the number of entries per refsection by hooking into \AtDataInput. In this solution we create a counter for each section. \refsectionbibcount is just a convenient hook to retrieve the counter for the current refsection. Note that this solution requires that the heading be typeset inside the relevant refsection, but that is no problem here.

\documentclass{article}
\usepackage[style=alphabetic]{biblatex}

\makeatletter
\AtDataInput{%
\ifltxcounter{datacount@\the\c@refsection}
{}
{\newcounter{datacount@\the\c@refsection}}%
\stepcounter{datacount@\the\c@refsection}}

\newcommand*{\refsectionbibcount}{\arabic{datacount@\the\c@refsection}}
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@article{journal1,
author  = {Author 1},
title   = {Title 1},
journal = {Journal 1},
year    = 1993,
number  = 2,
pages   = {201-213},
month   = 7,
volume  = 4
}
@inproceedings{conference1,
title        = {Title 1},
year         = 1993,
author       = {Author 1},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 1},
publisher    = {Publisher 1},
}
@inproceedings{conference2,
title        = {Title 2},
year         = 1993,
author       = {Author 2},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 2},
publisher    = {Publisher 2},
}
@inproceedings{conference3,
title        = {Title 3},
year         = 1993,
author       = {Author 3},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 3},
publisher    = {Publisher 3},
}
\end{filecontents*}

\begin{document}
\begin{refsection}
\nocite{journal1}
\end{refsection}
\begin{refsection}
\nocite{conference1}
\nocite{conference2}
\nocite{conference3}
\end{refsection}
\end{document}


Your use case looks as though you are trying to count per entry type and not per refsection, that is also possible. Since you can split your bibliography by type without resorting to refsections things are a bit simpler here.

\documentclass{article}
\usepackage[style=alphabetic]{biblatex}

\makeatletter
\AtDataInput{%
\ifltxcounter{entrycount@\thefield{entrytype}}
{}
{\newcounter{entrycount@\thefield{entrytype}}}%
\stepcounter{entrycount@\thefield{entrytype}}}

\newcommand*{\entrycount}[1]{\arabic{entrycount@#1}}

\newcommand*{\printtypebib}[2]{%
\printbibliography[type=#1, title={#2 (\entrycount{#1})},
}
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@article{journal1,
author  = {Author 1},
title   = {Title 1},
journal = {Journal 1},
year    = 1993,
number  = 2,
pages   = {201-213},
month   = 7,
volume  = 4
}
@inproceedings{conference1,
title        = {Title 1},
year         = 1993,
author       = {Author 1},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 1},
publisher    = {Publisher 1},
}
@inproceedings{conference2,
title        = {Title 2},
year         = 1993,
author       = {Author 2},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 2},
publisher    = {Publisher 2},
}
@inproceedings{conference3,
title        = {Title 3},
year         = 1993,
author       = {Author 3},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 3},
publisher    = {Publisher 3},
}
\end{filecontents*}

\begin{document}
\nocite{*}
\printtypebib{article}{Journal Articles}
\printtypebib{inproceedings}{Conference Articles}
\end{document}


Note that since we use \AtDataInput in both solutions, the count could be off in case the bibliography is filtered by additional criteria or there are some entries in the .bbl that are excluded from the bibliography. It should be possible to guard against these cases, but what needs to happen may depend on the situation at hand.

• It works. However, the publications are a part of my resume in which I have defined styles for each section. This is why I mentioned \printbibliography[heading=none] and used subsection{Journal Articles (1)} to write the heading. In realty, it is mysubsection{Journal Articles (1)}. I tried changing heading to none and subsection{Journal Articles (\refsectionbibcount)} but it throws error. I guess the macro can't be accessed outside its limited scope. – Ravi Joshi Jun 16 at 12:24
• @RaviJoshi Yes, as I wrote the macro must be accessed within the relevant refsection. You can say something like \defbibheading{bibnumbered}[\refname]{\mysubsection{#1}} to use \mysubsection instead of \subsection for the automatic headings. If you can't get that to work for whatever reason, it should be enough to say heading=none and move the call to \mysubsubsection inside the \begin{refsection}...\end{refsection}. – moewe Jun 16 at 12:32
• Thank you again for the workaround. Can you please demonstrate how to use \defbibheading in this scenario? – Ravi Joshi Jun 16 at 12:55
• @RaviJoshi Assuming \mysubsection works like \subsection you'd just write \defbibheading{subbibnumbered}[\refname]{\mysubsection{#1}} (I mistakenly put bibnumbered in the last comment, you'd probably want to say \defbibheading{bibnumbered}[\refname]{\mysection{#1}} for that) into your preamble and continue using the rest of the code as shown in the answer. – moewe Jun 16 at 12:59
• Both solutions are working like a charm. Sincere thanks for the explanation. – Ravi Joshi Jun 17 at 2:10

There is the lovely xcntperchap package to do exactly this:

\documentclass{article}
\usepackage[style=alphabetic]{biblatex}

\usepackage{filecontents}
\begin{filecontents*}{publications.bib}
@article{journal1,
author  = {Author 1},
title   = {Title 1},
journal = {Journal 1},
year    = 1993,
number  = 2,
pages   = {201-213},
month   = 7,
volume  = 4
}
@inproceedings{conference1,
title        = {Title 1},
year         = 1993,
author       = {Author 1},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 1},
publisher    = {Publisher 1},
}
@inproceedings{conference2,
title        = {Title 2},
year         = 1993,
author       = {Author 2},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 2},
publisher    = {Publisher 2},
}
@inproceedings{conference3,
title        = {Title 3},
year         = 1993,
author       = {Author 3},
volume       = 4,
series       = 5,
month        = 7,
organization = {Organization 3},
publisher    = {Publisher 3},
}
\end{filecontents*}

\newcounter{foo}
\renewbibmacro*{finentry}{\stepcounter{foo}\finentry}
\usepackage{xcntperchap}
\RegisterTrackCounter{subsection}{foo}

\begin{document}
\section{Publications}
\subsection{Journal Articles (\ObtainTrackedValueExp{subsection}{foo})}
\begin{refsection}
\setcounter{foo}{0}
\nocite{journal1}
\end{refsection}

\subsection{Conference Articles (\ObtainTrackedValueExp{subsection}{foo})}
\begin{refsection}
\setcounter{foo}{0}
\nocite{conference1}
\nocite{conference2}
\nocite{conference3}