44

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.

2
  • 4
    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
    Commented Nov 18, 2011 at 16:55
  • 2
    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
    Commented Nov 21, 2011 at 19:31

3 Answers 3

35

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]
    }
  }
}
9
  • Is there a way to specify the sourcemap to handle the case where some entries have keywords and some don't?
    – Audrey
    Commented Mar 7, 2012 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
    Commented Jan 17, 2014 at 16:16
  • I have tried this with all three config files (TeXworks, MikTeX 2.9, Win7 64 bit) but biblatex does not recognise 'hectic' and 'flutter' nor the keywords 'primary' and 'secondary': Package biblatex Warning: Keyword 'primary' not found on input line 32. etc. I followed the instructions here and compiled with pdfLaTeX+Biber+pdfLaTeX. Commented Mar 7, 2015 at 13:11
  • 2
    Since this question is quite popular (vote- and view-wise) would you mind updating your answer with a way to do the sourcemapping from within the document via \DeclareSourcemap. In my experience this is slightly less intimidating to some users than an external XML config file.
    – moewe
    Commented Jul 19, 2015 at 7:24
  • 2
    It's useful to note that the map_append functionality (like <map_step map_field_set="KEYWORDS" map_field_value="secondary" map_append="1"/>) is a "string append", so if you define any additional keywords by hand in the bib file, you should add a comma at the beginning of the automatically appended value (like map_field_value=",secondary"). Otherwise unexpected things will happen.
    – igneus
    Commented Feb 7, 2019 at 9:44
11

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}

\newcommand*{\citeprim}[2][]{\addtocategory{prim}{#2}\cite[#1]{#2}}
\newcommand*{\citesek}[2][]{\addtocategory{sek}{#2}\cite[#1]{#2}}

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.

4
  • 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
    Commented Nov 23, 2011 at 9:11
  • 2
    how to use the \nocite command for a particular category?
    – wiso
    Commented Feb 19, 2013 at 16:44
  • but then you would need to define an alternative parencite, autocite, citeyear, citeauthor, and so on, once for each biblio...
    – PatrickT
    Commented May 12, 2016 at 19:07
  • While that's probably not worth the effort when you write a five page paper, it certainly pays off for longer projects.
    – Simifilm
    Commented May 12, 2016 at 21:33
0

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)

See also p. 101 - 103 for usage examples

Though this would of course only suit your needs if your two bibliographies apply to two distinct sections only.

2
  • 2
    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
    Commented Apr 4, 2012 at 16:07
  • You're right, I misunderstood the question.
    – altabq
    Commented Nov 20, 2012 at 23:10

You must log in to answer this question.

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