# biblatex: multiple bibliographies categorised by different .bib files

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.

-
This is s good point. I will look into this for Biber as given that it's a datasource mapping task really, it should be handled there. Shouldn't be too hard to hard to add a config file mapping facility which will add arbitrary fields to entries in only certain files. –  PLK Nov 18 '11 at 16:55
A quick update. I have this working nicely now with biber. I will upload development binaries in the next couple of days. I'll also update this question with a full solution demonstration. –  PLK Nov 21 '11 at 19:31

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}

\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>

-
Is there a way to specify the sourcemap to handle the case where some entries have keywords and some don't? –  Audrey Mar 7 '12 at 16:26
See edits. Also, this can all be done at the document level using the \DeclareSourcemap macro in biblatex (this macro writes XML like the above to the .bcf file). –  PLK Jan 17 at 16:16

IMO the best way to deal with this is not on the level of the .bib file but with special \cite commands which allow you define ad hoc which entry goes into which bibliography. Working with two different .bib files for the two bibliographies means that you have to decide once and for all in advance which entry goes where, which isn't smart IMO.

I do something like this:

\DeclareBibliographyCategory{sek}
\DeclareBibliographyCategory{prim}



Now if you want to add an entry to your bibliography of primary sources, you can just type \citeprim{citekey}. You have to set up all the \cite command you're going to use this way. But at least for me this is makes much more sense since I only have to care about which entry goes where when I'm actually working on the document.

-
I agree that this is the best (only) solution if your references aren't divided by datasource. If you can categorise them by datasource separation though, doing this while reading the data is a lot easier as you don't have to decide which cite command to use for each entry. –  PLK Nov 23 '11 at 9:11
how to use the \nocite command for a particular category? –  wiso Feb 19 '13 at 16:44

Isn't this also possible with the refsegment implementation? From the Biblatex documentation:

The refsegment environment is used in the document body to mark a reference segment. This environment is useful if you want one global bibliography which is subdivided by chapter, section, or any other part of the document. (p 74)

No, it isn't possible. The OP was asking for a way to filter items in a bibliography by resource (bib file). refsegment filters a bibliography by document segment. refsection can be limited to a specific resource, but it is only useful for independent bibliographies. Primary/secondary categories probably don't fit into this special case. –  Audrey Apr 4 '12 at 16:07