[Tex/LaTex] biblatex: multiple bibliographies categorised by different .bib files

biberbiblatexsubdividing

biblatex supports multiple bibliographies by looking up the 1) entry type, 2) keyword, or 3) category.

This is all fine, but in the use case of primary sources and secondary sources:
1) the 'type' method doesn't work,
2) it is quite tedious to key in the 'primary' and 'secondary' keywords for each .bib entry, and
3) declaring the sorting by category would entail typing all the primary keys in the document.

So, if I'm storing all my primary bib entries in one .bib file, and secondary one in another, is there a bit to tell biblatex to assign a keyword or category automatically to all the entries from that bib file as it imports them? Done this way, entries can be easily swapped to and from the primary and secondary .bib files without fiddling individual keywords, and I don't have to type out all the primary and/or secondary entries into categories in each document.

Best Answer

This is now implemented in Biber 0.9.8. Here is how to deal with your question. Given the sample file:

\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}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[backend=biber,style=authoryear]{biblatex}
\usepackage{csquotes}
\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}

You can automatically add the correct keywords to your data as Biber reads it by using the following biber.conf file:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <sourcemap>
    <maps datatype="bibtex" bmap_overwrite="1">
      <map>
        <per_datasource>test-primary.bib</per_datasource>
        <map_step map_field_set="KEYWORDS" map_field_value="primary"/>
      </map>
      <map>
        <per_datasource>test-secondary.bib</per_datasource>
        <map_step map_field_set="KEYWORDS" map_field_value="secondary"/>
      </map>
    </maps>
  </sourcemap>
</config>

This will add the necessary keywords based on the datasource names. If you already had a KEYWORD field in your datasources which you wanted to keep, you could use the match/replace functionality instead:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <sourcemap>
    <maps datatype="bibtex" map_overwrite="1">
      <map>
        <per_datasource>test-primary.bib</per_datasource>
        <map_step map_field_source="KEYWORDS" map_match="^" map_replace="primary,"/>
      </map>
      <map>
        <per_datasource>test-secondary.bib</per_datasource>
        <map_step map_field_source="KEYWORDS" map_match="^" map_replace="secondary,"/>
      </map>
    </maps>
  </sourcemap>
</config>

The Biber manual has been updated in 0.9.8 with documentation of the config file format and has more examples. The format of the user config file is a major change from 0.9.6 (and some changes also from 0.9.7) as the older format was a bit of a mess and couldn't be extended to cope with situations like this due to inherent limitations of the format itself. I opted for a real XML format for flexibility. You can also validate your config file now by passing the --validate_config option to Biber.

EDIT: Since biber 1.3, you there is an "append" mode for setting values in sourcemaps so it's easier like this, which deals with entries with or without an existing KEYWORDS field when you want to keep any existing values:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <sourcemap>
    <maps datatype="bibtex" bmap_overwrite="1">
      <map>
        <per_datasource>test-primary.bib</per_datasource>
        <map_step map_field_set="KEYWORDS" map_field_value="primary" map_append="1"/>
      </map>
      <map>
        <per_datasource>test-secondary.bib</per_datasource>
        <map_step map_field_set="KEYWORDS" map_field_value="secondary" map_append="1"/>
      </map>
    </maps>
  </sourcemap>
</config>

Here is the same thing using the biblatex macro interface which you can put directly in your document:

\DeclareSourcemap{
  \maps[datatype=bibtex, overwrite]{
    \map{
      \perdatasource{test-primary.bib}
      \step[fieldset=KEYWORDS, fieldvalue=primary, append]
    }
    \map{
      \perdatasource{test-secondary.bib}
      \step[fieldset=KEYWORDS, fieldvalue=secondary, append]
    }
  }
}
Related Question