TransWikia.com

Biblatex with multiple categories across different refsections

TeX - LaTeX Asked on August 24, 2021

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.bibcitex[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.bibcitex[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 ofprintbibliography. 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?

One Answer

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
defblx@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@thec@refsection @#2}
    {blx@auxwrite@mainaux{}{stringabx@aux@category{#1}{csuse{blx@setc@thec@refsection @#2}}}%
     abx@aux@category{#1}{csuse{blx@setc@thec@refsection @#2}}}
    {}%
  blx@auxwrite@mainaux{}{stringabx@aux@category{#1}{#2}}%
  abx@aux@category{#1}{#2}%
  %add ref categories
  blx@auxwrite@mainaux{}{stringabx@aux@category{thec@refsection-refsection-#1}{#2}}%
  abx@aux@category{thec@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}{expandafterblx@key@categoryexpandafter{thec@refsection-refsection-#1}}
define@key{blx@biblist2}{refcategory}{expandafterblx@key@categoryexpandafter{thec@refsection-refsection-#1}}  
define@key{blx@bib2}{notrefcategory}{expandafterblx@key@notcategoryexpandafter{thec@refsection-refsection-#1}}
define@key{blx@biblist2}{notrefcategory}{expandafterblx@key@notcategoryexpandafter{thec@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.bibcitex[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.bibcitex[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}

Correct answer by Ulrike Fischer on August 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP