[Tex/LaTex] biblatex defernumbers per refsection

biblatexsubdividing

In one refsection of my document the references are grouped into categories by their keywords. Now I want to print the bibliography for every section based on the category of the citation, e.g. one bibliography for articles with keyword in and one bibliography with keyword out. For aesthetic I want the bibliographies to be numbered continuously, like in this textual mockup

**In**
[1-1] a. “a”. In: a (a).
[1-2] c. “c”. In: c (c).
[1-3] e. “e”. In: e (e).
**Out**
[1-4] b. “b”. In: b (b).
[1-5] d. “d”. In: d (d).

Hence, I use the defernumbers of biblatex to number the entries continuously. The problem is, that this leads to the numbering starting with 1 again for every \printbibliography.

I'd like to enable defernumbers across \printbibliography per \refsection.

MWE

\documentclass{article}
\pagestyle{empty}
\usepackage{filecontents}
\begin{filecontents*}{test.bib}
@article{a,
  title = {a},
  author = {a},
  journal = {a},
  year = {a},
  keywords = {in}
}
@article{b,
  title = {b},
  author = {b},
  journal = {b},
  year = {b},
  keywords = {out}
}
@article{c,
  title = {c},
  author = {c},
  journal = {c},
  year = {c},
  keywords = {in}
}
@article{d,
  title = {d},
  author = {d},
  journal = {d},
  year = {d},
  keywords = {out}
}
@article{e,
  title = {e},
  author = {e},
  journal = {e},
  year = {e},
  keywords = {in}
}
\end{filecontents*}
\usepackage[
  defernumbers=true,
  citestyle=numeric,
]{biblatex}
\defbibheading{subbibliography}[\bibname]{%
  \subsubsection*{#1}
}
\addbibresource{test.bib}
\begin{document}
\section{Testing biblatex}
\refsection
\cite{a,b,c,d,e}
\printbibliography[
  title={In},
  prefixnumbers={\thesection-},
  heading=subbibliography,
  keyword=in
]
\printbibliography[
  title={Out},
  prefixnumbers={\thesection-},
  heading=subbibliography,
  keyword=out
]
\endrefsection

\section{Testing biblatex}
\refsection
\cite{a,b,c,d,e}
\printbibliography[
  title={In},
  prefixnumbers={\thesection-},
  heading=subbibliography,
  keyword=in
]
\printbibliography[
  title={Out},
  prefixnumbers={\thesection-},
  heading=subbibliography,
  keyword=out
]
\endrefsection
\end{document}

Output

enter image description here


Of course, bounty will be offered!

Best Answer

This problem arises from an inherent functionality of biblatex. As seen from other questions, biblatex has an implicit resetnumbers=true that is applied when you use prefixnumbers to add a prefix to the label. This can be addressed in different ways, but only through hacks that really either change the problem slightly or alter the way the prefix is handled/applied.

Since the intent of this question is to subdivide each bibliography by section (or chapter, etc), it is possible to apply the prefixnumber setting globally in a different fashion, eliminating the implicit resetnumbers=true that is applied at each \printbibliography. To do so, we redefine the way the label is printed, incorporating \thesection into the format:

\DeclareFieldFormat{labelnumber}{\mkbibsecnum{#1}}
\newrobustcmd{\mkbibsecnum}[1]{\thesection-#1\relax}

Each label is stored as 1, 2, 3... but is formatted with the section number that it appears in when it is printed. (Note*: because we are adding the prefix in this way, the \printbibliography command must appear in the same section/refsection as the citations. In particular this means that you cannot use this method to print a cumulative bibliography at the end with multiple refsections subdivided out with \printbibliography[section=1...]).

At this point, all that remains is to reset the numbering at the proper point (the first bibliography of each refsection) with \printbibliography[...resetnumbers=true].

Result

The result appears as:

subdivided bibliographies

MWE

I have made a few changes to make the solution more efficient than in the original MWE.

refsection=section (could also be ...=part, ...=chapter, etc) is passed globally to biblatex to collect references only in each section. This can be replaced with \newrefsection/\endrefsection as appropriate to manually partition the bibliography items.

defernumbers=true is retained to sort each subbibliography sequentially.

I also commented out a few of the citations to accentuate the effect of the numbering and refsections.

The modified MWE demonstrating this solution:

\documentclass{article}
\pagestyle{empty}
\usepackage{filecontents}
\begin{filecontents*}{test.bib}
@article{a,
  title = {a},
  author = {a},
  journal = {a},
  year = {a},
  keywords = {in}
}
@article{b,
  title = {b},
  author = {b},
  journal = {b},
  year = {b},
  keywords = {out}
}
@article{c,
  title = {c},
  author = {c},
  journal = {c},
  year = {c},
  keywords = {in}
}
@article{d,
  title = {d},
  author = {d},
  journal = {d},
  year = {d},
  keywords = {out}
}
@article{e,
  title = {e},
  author = {e},
  journal = {e},
  year = {e},
  keywords = {in}
}
\end{filecontents*}
\usepackage[
  defernumbers=true,
  citestyle=numeric,
  refsection=section % Each \section{...} starts a new refsection environment
]{biblatex}

% Format the labelnumber with \thesection prefix
\DeclareFieldFormat{labelnumber}{\mkbibsecnum{#1}}
\newrobustcmd{\mkbibsecnum}[1]{\thesection-#1\relax}

\defbibheading{subbibliography}[\bibname]{%
  \subsubsection*{#1}
}
\addbibresource{test.bib}
\begin{document}

\section{Testing biblatex} % New refsection, too!
Cite a \cite{a};
Cite b \cite{b};
Cite c \cite{c};
\printbibliography[
  title={In},
  heading=subbibliography,
  keyword=in,
  resetnumbers=true % The first bibliography in each refsection needs its numbers manually reset
]
\printbibliography[
  title={Out},
  heading=subbibliography,
  keyword=out
]

\section{Testing biblatex} % New refsection, too!
Cite b \cite{b};
Cite d \cite{d};
Cite e \cite{e}.
\printbibliography[
  title={In},
  heading=subbibliography,
  keyword=in,
  resetnumbers=true % The first bibliography in each refsection needs its numbers manually reset
]
\printbibliography[
  title={Out},
  heading=subbibliography,
  keyword=out
]
\end{document}

*@Guido's solution redefines the entire bibliography environment and uses the enumerate series counter to continue counting for certain labels but not others. This might work for this problem (and would fix the issue of the cumulative bibliography), but I was unable to get the code to compile with section numbering the way the OP asked.