1

With biblatex and biber, I use refsections to create multiple bibliographies. In addition I use categories to further split up the bibliographies. For each refsection I use a different bib file, which are not under my control, keys might be duplicate across different bib files.

The problem appears, when an entry is added to a category within the first refsection and another entry with the same key is added to another category in the next refsection. In the second refsection the entry is then the wrong category (as assigned within the first refsection).

Here is a running example

\documentclass{article}
\usepackage[backend=biber,defernums=true,doi=true]{biblatex}

\DeclareBibliographyCategory{own}
\DeclareBibliographyCategory{other}
\DeclareBibliographyCategory{important}

\defbibheading{first}{\section{First Bib}}
\defbibheading{second}{\section{Second Bib}}

\newcommand{\citex}[2][own]{%
    \addtocategory{#1}{#2}%
    \cite{#2}
}

\begin{filecontents}{aaa.bib}
    @article{a,
    author = {A Author},
    title = {a from aaa.bib},
    journal = {Journal of aaa.bib}}
\end{filecontents}

\begin{filecontents}{bbb.bib}
    @article{a,
    author = {A Author},
    title = {a from bbb.bib},
    journal = {Journal of bbb.bib}}

    @article{b,
    author = {B Author},
    title = {b from bbb.bib},
    journal = {Journal of bbb.bib}}

    @article{c,
    author = {C Author},
    title = {c from bbb.bib},
    journal = {Journal of bbb.bib}}

\end{filecontents}

\addbibresource[label=alabel]{aaa.bib}
\addbibresource[label=blabel]{bbb.bib}

\begin{document}

\begin{refsection}[alabel]
    \section{First refsection}
    First refsection with only contents of aaa.bib\citex[own]{a}
    \printbibliography[section=\therefsection,heading=first, category=own]

    Here comes the second Bibliography
    \printbibliography[section=\therefsection,heading=second,notcategory=own]

\end{refsection}

\begin{refsection}[blabel]
    \section{Second refsection}
    Second refsection with only contents of bbb.bib\citex[own]{b}\citex[important]{a},\citex[other]{c}
    \printbibliography[section=\therefsection,heading=first, category=own]

    Here comes the second Bibliography
    \printbibliography[section=\therefsection,heading=second,notcategory=own]

\end{refsection}
\end{document}

The above code results in the following output

The reference 1 "a from bbb.bib" is not added to the category "own" but to "important, which should be printed in the second bibliography (Section 5) instead of the first bibliography (Section 4)

The biblatex manual states

\addtocategory{〈category〉}{〈key〉} Assigns a〈key〉to a〈category〉, to be used in conjunction with the category and notcategory filters of\printbibliography. This command may be used inthe preamble and in the document body. The〈key〉may be a single entry key or a comma-separated list of keys. The assignment is global.

Is there any way of keeping the categories local to the refsections?

8
  • 1
    you could probably record the assignments and clean them up at the \end{refsection}. The alternative is to use e.g. the ref section counter, so that you actually have categories own\therefsection etc. Jul 21, 2020 at 9:38
  • @UlrikeFischer Do you have any idea on how to do the cleanup?
    – Frank
    Jul 21, 2020 at 13:21
  • after looking at the code I don't think it will work easily, the category is written to the aux-file, and read in in the next run so it would be difficult to keep track of it. Better define refsection related categories. Jul 21, 2020 at 13:34
  • 1
    hm. not so easy either. You can't define categories on the fly in the refsections. This needs some more thoughts. Jul 21, 2020 at 13:41
  • At least on first glance, it's a bit odd conceptually that categories are not local to refsections: almost everything else is (or should be). I don't know whether there is a deeper reason for that, but I probably won't have time to look into that before the weekend.
    – moewe
    Jul 22, 2020 at 6:09

1 Answer 1

1

You could try the following. It adds everything to categories numbered by refsection, you can then filter with the key refcategory:

\documentclass{article}
\usepackage[backend=biber,defernumbers=true,doi=true]{biblatex}

 \DeclareBibliographyCategory{own}
 \DeclareBibliographyCategory{other}
 \DeclareBibliographyCategory{important}


\makeatletter
\def\blx@addtocategory#1#2{%
  % If we are adding a set member to a category, make sure we also add the
  % set parent if it is cited. If it is cited, we should be using the set
  % parent in the category, not the set child member. blx@setc@... will
  % only exist if the set parent is also cited as it is only defined when a
  % member contains \inset which only happens when the set parent is cited.
  \ifcsdef{blx@setc@\the\c@refsection @#2}
    {\blx@auxwrite\@mainaux{}{\string\abx@aux@category{#1}{\csuse{blx@setc@\the\c@refsection @#2}}}%
     \abx@aux@category{#1}{\csuse{blx@setc@\the\c@refsection @#2}}}
    {}%
  \blx@auxwrite\@mainaux{}{\string\abx@aux@category{#1}{#2}}%
  \abx@aux@category{#1}{#2}%
  %add ref categories
  \blx@auxwrite\@mainaux{}{\string\abx@aux@category{\the\c@refsection-refsection-#1}{#2}}%
  \abx@aux@category{\the\c@refsection-refsection-#1}{#2}%
  }

\define@key{blx@bib1}{refcategory}{}
\define@key{blx@bib1}{notrefcategory}{}
\define@key{blx@biblist1}{refcategory}{}
\define@key{blx@biblist1}{notrefcategory}{}
\define@key{blx@bib2}{refcategory}{\expandafter\blx@key@category\expandafter{\the\c@refsection-refsection-#1}}
\define@key{blx@biblist2}{refcategory}{\expandafter\blx@key@category\expandafter{\the\c@refsection-refsection-#1}}  
\define@key{blx@bib2}{notrefcategory}{\expandafter\blx@key@notcategory\expandafter{\the\c@refsection-refsection-#1}}
\define@key{blx@biblist2}{notrefcategory}{\expandafter\blx@key@notcategory\expandafter{\the\c@refsection-refsection-#1}}
\makeatother  

\defbibheading{first}{\section{First Bib}}
\defbibheading{second}{\section{Second Bib}}

\newcommand{\citex}[2][own]{%
    \addtocategory{#1}{#2}%
    \cite{#2}
}

\begin{filecontents}{aaa.bib}
    @article{a,
    author = {A Author},
    title = {a from aaa.bib},
    journal = {Journal of aaa.bib}}
\end{filecontents}

\begin{filecontents}{bbb.bib}
    @article{a,
    author = {A Author},
    title = {a from bbb.bib},
    journal = {Journal of bbb.bib}}

    @article{b,
    author = {B Author},
    title = {b from bbb.bib},
    journal = {Journal of bbb.bib}}

    @article{c,
    author = {C Author},
    title = {c from bbb.bib},
    journal = {Journal of bbb.bib}}

\end{filecontents}

\addbibresource[label=alabel]{aaa.bib}
\addbibresource[label=blabel]{bbb.bib}


\begin{document}

\begin{refsection}[alabel]
    \section{First refsection}
    First refsection with only contents of aaa.bib\citex[own]{a}
    \printbibliography[section=\therefsection,heading=first, refcategory=own]

    Here comes the second Bibliography
    \printbibliography[section=\therefsection,heading=second,notrefcategory=own]

\end{refsection}

\begin{refsection}[blabel]
    \section{Second refsection}
    Second refsection with only contents of bbb.bib\citex[own]{b}\citex[important]{a},\citex[other]{c}
    \printbibliography[section=\therefsection,heading=first, refcategory=own]

    Here comes the second Bibliography
    \printbibliography[section=\therefsection,heading=second,notrefcategory=own]

\end{refsection}
\end{document}
1
  • Thanks a lot, this works nicely. Could you explain the solution a bit? Also, does this scale up to an arbitrary number of refsections?
    – Frank
    Jul 22, 2020 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .