Is it possible to add entries to the bibliography based on keyword using Biblatex/Biber and within the document code?

Update: This is now possible with biber 2.14/biblatex 3.14, both in their respective development folders on github [1][2]:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@book{new-stuff,
    author          =   {Watt, Brian},
    title           =   {Happy Times with Penguins},
    publisher       =   {Harvard University Press},
    address         =   {Cambridge, MA},
    year            =   1995,
    pagination      =   {section},
    keywords        =   {happy}}
@book{den-coll,
  author                =   {Till, Jr., Dennis E.},
    booktitle           =   {Penguin Land and Further North: Human Influence},
    title               =   {Penguin Land and Further North: Human Influence},
    publisher           =   {Oxford University Press},
    year                =   2008,
    address             =   {Oxford and New York},
    keywords            =   {penguin, human}}
@book{old-stuff,
  author                =   {Harvey, Jr., Dennis E.},
    booktitle           =   {Penguin Land and Further North: Human Influence},
    title               =   {Penguin Land and Further North: Human Influence},
    publisher           =   {Someone \& Daughters},
    year                =   1567,
    address             =   {Oxford},
    bookpagination      =   {paragraph},
    keywords            =   {penguin}}
\end{filecontents*}

\usepackage[defernumbers=true]{biblatex}
\addbibresource{\jobname.bib}
\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[nocited, final]
      \step[fieldsource=keywords, notmatch=happy, final]
      \step[entrynull]
    }
  }
}

\begin{document}

\cite{old-stuff}

\nocite{*}
\printbibliography

\end{document}

This removes entries during parsing the data sources which are \nocited or which don't have the keyword.

enter image description here


This is an old question, and it doesn't look like there is a really satisfactory answer. But here's a solution that involves a perl script just for fun.

Remember to run it with pdflatex --enable-write18.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{allentries.bib}
@book{new-stuff,
    author          =   {Watt, Brian},
    title           =   {Happy Times with Penguins},
    publisher       =   {Harvard University Press},
    address         =   {Cambridge, MA},
    year            =   1995,
    pagination      =   {section},
    keywords        =   {happy}}
@book{den-coll,
  author                =   {Till, Jr., Dennis E.},
    booktitle           =   {Penguin Land and Further North: Human Influence},
    title               =   {Penguin Land and Further North: Human Influence},
    publisher           =   {Oxford University Press},
    year                =   2008,
    address             =   {Oxford and New York},
    keywords            =   {penguin, human}}
@book{old-stuff,
  author                =   {Harvey, Jr., Dennis E.},
    booktitle           =   {Penguin Land and Further North: Human Influence},
    title               =   {Penguin Land and Further North: Human Influence},
    publisher           =   {Someone \& Daughters},
    year                =   1567,
    address             =   {Oxford},
    bookpagination      =   {paragraph},
    keywords            =   {penguin}}
\end{filecontents*}
\begin{filecontents*}{citekeyword.pl}
use strict;
use warnings;
use Text::BibTeX;
my $keyword = "happy"; # set keyword here
my $bibfile = Text::BibTeX::File->new("allentries.bib");
my $keywordcitefile = "keywordcites.tex";
open(my $fh, '>', $keywordcitefile) or
    die "Could not open file '$keywordcitefile' $!";
my $keywords;
my $entry;
while ($entry = Text::BibTeX::Entry->new($bibfile)) {
    next unless $entry->parse_ok;
    if ($entry->get("keywords")) {
        $keywords = "," . $entry->get("keywords") . ",";
        if ($keywords =~ /,$keyword,/) {
            print $fh "\\nocite{" . $entry->key . "}\n";
        }
    }
}
close $fh;
\end{filecontents*}
\immediate\write18{/usr/bin/perl citekeyword.pl}
\usepackage[defernumbers=true]{biblatex}
\addbibresource{allentries.bib}
\pagestyle{empty}
\begin{document}
\input{keywordcites.tex}
\cite{old-stuff}
\printbibliography
\end{document}

enter image description here

Tags:

Biblatex

Biber