0

I am using biblatex with Biber. I am using categories to separate citations that belong to different types and to produce different bibliographies accordingly. Let's say I have a.bib file that contains bib records (let's say with labels a1, a2, ...) that all belong to a category c1, and a b.bib file that contains bib records (labels b1, b2, ...) that all belong to category c2. Currently, I write something like this:

\usepackage[citestyle=numeric-comp,backend=biber,defernumbers=true]{biblatex}
\DeclareBibliographyCategory{c1}
\DeclareBibliographyCategory{c2}
\addtocategory{c1}{a1,a2,...}
\addtocategory{c2}{b1,b2,...}
\addbibresource{a.bib}
\addbibresource{b.bib}

Is there an easy way to say: "all bib records in a.bib belong to c1, without listing them all?"

2

biblatex offers two main solutions to the task of splitting/categorising entries into arbitrary subsets

  1. bibliography categories and
  2. keywords.

Bibliography categories work on the fly on the document side. You declare categories with \DeclareBibliographyCategory and add entries to categories with \addtocategory in your document. Categories are useful for ad-hoc classification and grouping that may vary from document to document. One great example of categories is How to split bibliography into "works cited" and "works not cited"?, where categories are used to split the bibliography in cited works and those added with \nocite.

Keywords on the other hand are added to the .bib file in the keywords field. They require no further set-up in the document and can be used directly to filter bibliographies with keyword=<keyword> or for general purpose tests with \ifkeyword{<keyword>}{<true>}{<false>}. Since keywords come from the .bib file, they are usually tied to a specific entry and not to a document.

For the most part, categories and keywords can be used in a similar fashion once they are known/defined.


When Biber/BibTeX has processed your .bib file and passes the data on to biblatex in the .bbl there is no information about the data source of each entry. That makes it extremely hard to generate categories from .bib files, since the relevant data is not available any more.

There is, however, a trick to let Biber add different keywords for each .bib file as shown in biblatex: multiple bibliographies categorised by different .bib files.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\usepackage[backend=biber,style=authoryear]{biblatex}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \perdatasource{\jobname-primary.bib}
      \step[fieldset=keywords, fieldvalue={, primary}, append]
    }
    \map{
      \perdatasource{\jobname-secondary.bib}
      \step[fieldset=keywords, fieldvalue={, secondary}, append]
    }
  }
}

\usepackage{filecontents}
\begin{filecontents}{\jobname-primary.bib}
@BOOK{hectic,
  AUTHOR    = {Henry Hectic},
  TITLE     = {How Horticulturalists Howl},
  PUBLISHER = {Honorary Books: Henage},
  YEAR      = {2000}
}
\end{filecontents}
\begin{filecontents}{\jobname-secondary.bib}
@BOOK{flutter,
  AUTHOR    = {Frederick Flutter},
  TITLE     = {Fraternising with Flowers},
  PUBLISHER = {Frippery Pamphlets: Folkestone},
  YEAR      = {1995}
}
\end{filecontents}

\addbibresource{\jobname-primary.bib}
\addbibresource{\jobname-secondary.bib}

\begin{document}
Some citations: \cite{hectic}, \cite{flutter}.
\printbibliography[title=Primary Sources, keyword=primary]
\printbibliography[title=Secondary Sources, keyword=secondary]
\end{document}

If you insist on categories, this can be used as a roundabout way to produce categories in the end as follows.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\usepackage[backend=biber,style=authoryear]{biblatex}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \perdatasource{\jobname-primary.bib}
      \step[fieldset=keywords, fieldvalue={, primary}, append]
    }
    \map{
      \perdatasource{\jobname-secondary.bib}
      \step[fieldset=keywords, fieldvalue={, secondary}, append]
    }
  }
}

\DeclareBibliographyCategory{primary}
\DeclareBibliographyCategory{secondary}

\AtDataInput{%
  \ifkeyword{primary}
    {\addtocategory{primary}{\thefield{entrykey}}}
    {}%
  \ifkeyword{secondary}
    {\addtocategory{secondary}{\thefield{entrykey}}}
    {}%    
}

\usepackage{filecontents}
\begin{filecontents}{\jobname-primary.bib}
@BOOK{hectic,
  AUTHOR    = {Henry Hectic},
  TITLE     = {How Horticulturalists Howl},
  PUBLISHER = {Honorary Books: Henage},
  YEAR      = {2000}
}
\end{filecontents}
\begin{filecontents}{\jobname-secondary.bib}
@BOOK{flutter,
  AUTHOR    = {Frederick Flutter},
  TITLE     = {Fraternising with Flowers},
  PUBLISHER = {Frippery Pamphlets: Folkestone},
  YEAR      = {1995}
}
\end{filecontents}

\addbibresource{\jobname-primary.bib}
\addbibresource{\jobname-secondary.bib}

\begin{document}
Some citations: \cite{hectic}, \cite{flutter}.
\printbibliography[title=Primary Sources, category=primary]
\printbibliography[title=Secondary Sources, category=secondary]
\end{document}

But I doubt that that is going to provide a great advantage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.