# “\thechapter” as prefixnumbers for bibliography with biblatex

I have a huge document and would like to do the following with biblatex:

1. Have one (actually, several) seperate bibliography for each chapter.
2. Add a prefix to each citation number to make them distinguishable.
3. Have that prefix be the (individual) chapter marker.

This works relatively smoothly for the bibliography itself, but does not work for citations since at the time these marks are generated, the \thechapter variable seems to be zero.

Minimal working example:

\documentclass{report}

\usepackage{filecontents}
\usepackage[backend=biber,defernumbers=true,citestyle=numeric]{biblatex}

\begin{filecontents}{bibliography.bib}
@book{citation,
title = {Title},
author = {Author, Some},
location = {The City},
publisher = {Publisher},
date = {2005}
}
\end{filecontents}

\begin{document}

\renewcommand{\thechapter}{A.7}
\section{Text}

This~\cite{citation} is a citation.

\end{document}


will give me:

-
Welcome to TeX.SX! Well-written question! A tip: You can use backticks  to mark your inline code as I did in my edit. –  yo' Feb 25 '14 at 13:17
Thanks for the nice welcome, and the helpful tip. I am just getting into biblatex for this huge document, and although very powerful, it is also rather complex for such a widely spread LaTeX project... I'm not even sure this question has an answer. –  Florian Fahrenberger Feb 25 '14 at 13:29
Well, biblatex is a powerful tool for very complicated process. it manages (almost) anything, but that means it can't be simple. Even the reference guide, it's good, but it's completely unreadable (like a reference guide to anything so complicated). –  yo' Feb 25 '14 at 14:35

The problem ist that biblatex doesn't expand fully the argument of prefixnumbers, and so \thechapter gets written literally to the aux file. You could add this to your preamble:

\makeatletter
\nottoggle{blx@skiplab}
{\ifdef\blx@prefixnumbers
{\ifundef\abx@field@shorthand
{\edef\abx@field@prefixnumber{\expandonce\blx@prefixnumbers}%
\ifundef\abx@field@prefixnumber
{prefixnumber}{\expandonce\abx@field@prefixnumber}}
{}%
\blx@auxwrite\@mainaux{}{%
\string\blx@aux@numprefix{\abx@field@entrykey}%
{\the\c@refsection}{\abx@field@prefixnumber}}}%<-- removed \expandonce
{}}
{}}
{}}
\makeatother


An alternative would be to change the \cite macro so that it doesn't use prefixnumbers but \thechapter:

\renewbibmacro*{cite}{%
\printtext[bibhyperref]{%
%\printfield{prefixnumber}% removed
\printfield{labelnumber}%
\ifbool{bbx:subentry}
{\printfield{entrysetcount}}
{}}}


In both cases I didn't test what will happen if you have more chapters and if some citations are in more than one chapter.

A third alternativ would be to change the key definition:

\makeatletter
\define@key{blx@bib2}{prefixnumbers}{%
\edef\blx@prefixnumbers{#1}%edef instead of def
\iftoggle{blx@defernumbers}
{\csnumdef{blx@labelnumber@\the\c@refsection}{0}}
{\iftoggle{blx@labelnumber}
{\blx@warning{%
Option 'prefixnumbers' requires global\MessageBreak
'defernumbers=true'}}
{}}}
\makeatother

-
Thanks a lot, Ulrike. This is a very manual fix, but I understand that biblatex is just (for good reasons) not meant to write directly into the .aux file. All three version as far as I can tell work with several chapters, at least for me. I don't have citations used in more than one chapter though (I seperated them into refsections with seperate .bib files). I will try this in its different variations over the next day. So far, I like version 1 the best, since the \cite command might be overwritten by hyperref or similar. –  Florian Fahrenberger Feb 25 '14 at 13:56
A shorter version for the first strategy is \patchcmd\blx@addprefixnumber{\expandonce\abx@field@prefixnumber}{\abx@field@pr‌​efixnumber}{}{} (of course between \makeatletter and \makeatother) –  egreg Feb 25 '14 at 13:59
Thanks, egreg. Oh, and of course any of these should be in the preamble after \usepackage[...]{biblatex}. :) Just in case this is unclear to future readers. –  Florian Fahrenberger Feb 25 '14 at 14:04
@FlorianFahrenberger: Version 2 didn't change \cite` but the bibmacro "cite". This will not interfere with hyperref etc. And biblatex does write directly in the aux-file but it doesn't expand the argument when writing: I don't think that it is really necessary to be so careful. The argument is normally simply a string and so it shouldn't do any harm to expand it. –  Ulrike Fischer Feb 25 '14 at 14:08
@UlrikeFischer Oh, thanks for clearing that up. I seriously did not expect such an expert answer so quickly. Awesome forum! –  Florian Fahrenberger Feb 25 '14 at 14:10