# \nocite{*} for single bibdatasources with biblatex/biber

Several bibliography resources are used in one single tex-document. They resources have been added using: \addbibresource{}. Now I'd like to use the \nocite{*} command to print all references of one of the bibresources. However \nocite{*} does consider all resources instead of just a single source (in the case of the example it should be resource2). Is there a way to specify that with \nocite{*}?

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-resource1.bib}
@article{A2014,
author={First Author},
journal={Journal A},
title={First Paper},
year={2014}
}
\end{filecontents*}
\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-resource2.bib}
@article{B2014,
author={Another Author},
journal={Journal B},
title={Second Paper},
year={2014}
}
@article{C2014,
author={Next Author},
journal={Journal C},
title={Third Paper},
year={2014}
}
\end{filecontents*}

\documentclass{article}

%bib

%%%%%%%%%%%%%%%
\begin{document}

This is a citation \cite{C2014}
\nocite{*}

\printbibliography

\end{document}

-
Is using \nocite{key} an option for you? (Probably not) –  moewe Apr 8 at 11:51
\nocite{key} is the last option (would be quite some manual work, to add and check references) –  Johannes Apr 8 at 12:59
Well, it is no problem to get biblatex to distinguish the two bibliography sources (via keywords or the like), but it seems to be quite hard to make biblatex \nocite a lot of entries but not all. –  moewe Apr 8 at 13:27
One problem for biblatex is that it is only able to see the entries once they are in the .bbl file, but in order for an entry to get into the .bbl file it will have to be \cited or \nocited. –  moewe Apr 8 at 13:54
Since I feel that really none of the solutions is the ne plus ultra answer, I have filed a bug report #228 on the biblatex bugtracker. –  moewe Apr 11 at 5:39

You can do this quite easily using reference sections with bound datasources:

\documentclass{article}
\usepackage[style=authoryear]{biblatex}
\begin{document}
\begin{refsection}[resource2.bib]
\nocite{*}
\printbibliography
\end{refsection}
\nocite{*}
\printbibliography
\end{document}


Here, the first \nocite only adds the references from resource2.bib because it's local to the refsection. The second \nocite only picks up things from resource1.bib because both are in refsection "'0". \printbibliography is local to a refsection if it has no section argument.

Here's another way of thinking about this and why it's hard in general. biblatex works semantically with bibliographies and so the way to cite things is by semantic information that applies to bibliographies - citekeys, entry types, reference sections etc. biber knows how to map these to things to a semantically lower level like files, tags in an XML file (the .bcf) and the like. The problem is that what is wanted is a biblatex way of specifying cite keys at a semantic level below that of "bibliography" e.g. "file". biblatex has no concept of such things in its code and it's very hard to implement (and would be very messy). It can pass through such foreign concepts like file names to biber but that's not enough for this case as biblatex internal data structures would need to track citation and sorting lists at the "file" semantic level and that's impossible. Simply put, there would need to be a bibliographic semantic component between "one or more citation keys" and "all citation keys in a refsection" which is all \nocite currently understands. "File" is already ruled out for reasons mentioned above.

-
But is there a solution to print the local bibliographies for refsection 0 and 1 in one \printbibliography command? (I think that is specifically what the OP was after). Just printing \printbibliography[section=0] and \printbibliograohy[section=1] one after the other (and maybe suppressing spacing and headings along the way) will seriously mess up sorting. –  moewe Apr 13 at 5:59
Hmm, that's basically impossible as a refsection is such a fundamental unit in biber. Sorting, disambiguation etc. is always calculated within a refsection. –  PLK Apr 13 at 8:13

The following approach is not entirely satisfying because technically for biblatex all entries will have been \(no)cited, this leads to biblatex applying disambiguation techniques it would not have to use.

The main idea is to use a sourcemap restricted to \jobname-resource2.bib

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


to add a keyword nocitethis to all the entries in that file.

We also define \bibcheck

\defbibcheck{mynocite}{%
\ifboolexpr{test {\ifciteseen} or test {\ifkeyword{nocitethis}}}
{}
{\skipentry}
}


This check will only print entries in the bibliography that either were cited before or have the specific keyword nocitethis that only entries from \jobname-resource2.bib have.

It remains to issue a \nocite{*} (so technically all entries are \nocited and processed by biber).

You will have to use the bibcheck in the bibliography

\printbibliography[check=mynocite]


We also will need to load biblatex with citetracker enabled in order for \ifciteseen to work

\usepackage[backend=biber,style=authoryear,citetracker=true]{biblatex}


MWE

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-resource1.bib}
@article{A2014,
author={First Author},
journal={Journal A},
title={First Paper},
note = {should appear in bib, because it was cited},
year={2014}
}
@article{D2014,
author={First Author},
journal={Journal A},
title={First Paper},
note = {should not be seen in bib},
year={2014}
}
\end{filecontents*}
\begin{filecontents*}{\jobname-resource2.bib}
@article{B2014,
author={Another Author},
journal={Journal B},
title={Second Paper},
note = {should appear in bib, b/c it is in \jobname-resource2 and nocite was issued},
year={2014}
}
@article{C2014,
author={Next Author},
journal={Journal C},
title={Third Paper},
note = {should appear in bib, b/c it is in \jobname-resource2 and nocite was issued},
year={2014}
}
\end{filecontents*}

\documentclass{article}

\usepackage[backend=biber,style=authoryear,citetracker=true]{biblatex}

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

\defbibcheck{mynocite}{%
\ifboolexpr{test {\ifciteseen} or test {\ifkeyword{nocitethis}}}
{}
{\skipentry}
}

\nocite{*}
\begin{document}
This is a citation \cite{A2014}

\printbibliography[check=mynocite]
\end{document}


You will notice that there is a "(2014a)" but no "(2014b)", for Biber "(2014b)" exists - it is D2014 - and has been cited (because of \nocite{*}), we just suppress it in the bibliography via our check.

-
Maybe the feature to apply \nocite to only a particular bib source is worth a feature request in the biblatex bugtracker. Also just another thought: Maybe one could cook something up with refsections, this might not work for one bibliography though. –  moewe Apr 8 at 14:05
The problem with disambiguation can be dealt with by adding the "dataonly" option to the options field for the entries you don't want considered for this (using DeclareSourcemap). –  PLK Apr 9 at 10:24
@moewe I think the difficulty is with biber and not biblatex since all biber see of nocite{*} is <bcf:citekey order="0">*</bcf:citekey>. I don't know enough about biber to know if it can handle additional options with a * cite. –  StrongBad Apr 9 at 12:14
@StrongBad Indeed it is Biber that applies the techniques, I maybe should have been clearer on that in the post. –  moewe Apr 9 at 13:43
@PLK I did not think of that, but is it possible to add dataonly to only those entries with true \citeseen? Adding that keyword to all entries of a file will not yield the expected results. –  moewe Apr 9 at 13:45

I second the suggestion that being able to 'nocite' all entries in a specific .bib file would be useful, but until that happens, it is not tricky to quickly grab those citations all in one go.

Imagine we want to populate a single file with all entry keys from a .bib file, so we can simply add a

\input{nocites}


That means we need a file called nocites.tex that has something like

% nocites.tex
\nocite{%
entrykey1, entrykey2, entrykey3, entrykey4, % ...
}


Using grep, sed, and tr, we can do this easily (and people better with regex will probably do it even more easily/efficiently):

# assuming a .bib file called bibliography.bib
grep @ bibliography.bib | grep -v '@string' | grep -v '\\\@' | sed 's/@.*{//g' | tr '\n' ' ' | sed 's/^/\\nocite{%\n/' | sed '\$a}' > nocites.tex


What this does is:

1. find each line that has an '@' in it;
2. discard any line that has a '@string' in it (optional);
3. discard any line that has a '\@' in it (optional; my .bib file would need this);
4. remove from each matching line the @<entrytype>{ (e.g., turn @Book{entry1 into entry1);
5. turn all new lines into a single line separated by spaces (optional);
6. prepend to the file a \nocite{% while moving the entry keys to the next line;
7. append a closing } to the end of the file;
8. write all these transformations to a file called nocites.tex.
-

This is difficult to achieve because of biber. When you do \nocite{*} this causes

<bcf:citekey order="0">*</bcf:citekey>


to be written to the bcf file which is read by biber and then used to create the bbl file which is used by biblatex. Without modifying biber, we are forced to get biblatex to write something in the bcf file that will cause biber to write something to the bbl file that can be modify with TeX.

...
\begin{document}
This is a citation \cite{C2014}
\begin{refsection}[\jobname-resource1.bib]
\nocite{*}
\end{refsection}
\printbibliography
\end{document}


results in

...
\refsection{0}
\sortlist{entry}{nyt}
\entry{C2014}{article}{}
\name{labelname}{1}{}{%
}
\name{author}{1}{}{%
}
\field{sortinit}{A}
\field{labelyear}{2014}
\field{datelabelsource}{}
\field{labeltitle}{Third Paper}
\field{journaltitle}{Journal C}
\field{title}{Third Paper}
\field{year}{2014}
\endentry
\endsortlist
\endrefsection

\refsection{1}
\sortlist{entry}{nyt}
\entry{A2014}{article}{}
\name{labelname}{1}{}{%
{{uniquename=0,hash=5ce4601588022c38e60ef07ac0267cd5}{Author}{A\bibinitperiod}{First}{F\bibinitperiod}{}{}{}{}}%
}
\name{author}{1}{}{%
{{uniquename=0,hash=5ce4601588022c38e60ef07ac0267cd5}{Author}{A\bibinitperiod}{First}{F\bibinitperiod}{}{}{}{}}%
}
\strng{namehash}{5ce4601588022c38e60ef07ac0267cd5}
\strng{fullhash}{5ce4601588022c38e60ef07ac0267cd5}
\field{sortinit}{A}
\field{labelyear}{2014}
\field{datelabelsource}{}
\field{labeltitle}{First Paper}
\field{journaltitle}{Journal A}
\field{title}{First Paper}
\field{year}{2014}
\endentry
\endsortlist
\endrefsection
\endinput


being written to the bbl. If we then hack the \refsection, \endsortlist, \endrefsection, and \sortlist macros at the right time we can get the desired output. Specifically we need

\usepackage{letltxmacro}
\makeatletter
\def\blx@bblfile@biber{%
\blx@secinit
\begingroup

\blx@bblstart
\blx@ifsigned{\jobname}{bbl}
{%
\LetLtxMacro{\refsectionOld}{\refsection}
\LetLtxMacro{\endsortlistOld}{\endsortlist}
\LetLtxMacro{\endrefsectionOld}{\endrefsection}
\LetLtxMacro{\sortlistOld}{\sortlist}
\def\refsection{\def\refsection{\@gobble}\refsectionOld}
\def\endsortlist{\def\endsortlist{\endsortlistOld}}
\def\endrefsection{\def\endrefsection{\endrefsectionOld}}
\def\sortlist{\def\sortlist{\@gobbletwo}\sortlistOld}
\InputIfFileExists{\jobname.bbl}
{\blx@info@noline{... file '\jobname.bbl' found}}
\typeout{No file \jobname.bbl.}}}
{}%
\blx@bblend
\endgroup
\csnumgdef{blx@labelnumber@\the\c@refsection}{0}}

\makeatother


Putting it altogether into a MWE

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-resource1.bib}
@article{A2014,
author={First Author},
journal={Journal A},
title={First Paper},
year={2014}
}
\end{filecontents*}
\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-resource2.bib}
@article{B2014,
author={Another Author},
journal={Journal B},
title={Second Paper},
year={2014}
}
@article{C2014,
author={Next Author},
journal={Journal C},
title={Third Paper},
year={2014}
}
\end{filecontents*}

\documentclass{article}

%bib

\usepackage{letltxmacro}
\makeatletter
\def\blx@bblfile@biber{%
\blx@secinit
\begingroup

\blx@bblstart
\blx@ifsigned{\jobname}{bbl}
{%
\LetLtxMacro{\refsectionOld}{\refsection}
\LetLtxMacro{\endsortlistOld}{\endsortlist}
\LetLtxMacro{\endrefsectionOld}{\endrefsection}
\LetLtxMacro{\sortlistOld}{\sortlist}
\def\refsection{\def\refsection{\@gobble}\refsectionOld}
\def\endsortlist{\def\endsortlist{\endsortlistOld}}
\def\endrefsection{\def\endrefsection{\endrefsectionOld}}
\def\sortlist{\def\sortlist{\@gobbletwo}\sortlistOld}
\InputIfFileExists{\jobname.bbl}
{\blx@info@noline{... file '\jobname.bbl' found}}
\typeout{No file \jobname.bbl.}}}
{}%
\blx@bblend
\endgroup
\csnumgdef{blx@labelnumber@\the\c@refsection}{0}}

\makeatother

%%%%%%%%%%%%%%%
\begin{document}
This is a citation \cite{C2014}
\begin{refsection}[\jobname-resource1.bib]
\nocite{*}
\end{refsection}
\printbibliography
\end{document}


This completely disables the ability to have multiple reference sections. You should be able to hack the macros to restore themselves at the correct time if you need multiple reference sections.

-
+1 Cool idea. But doesn't that solution have a problem with disambiguations as well? I cannot tell whether "Author 2014" refers to "First Paper" or "Third Paper". –  moewe Apr 9 at 13:47
Mhhh, if I cite just one of the entries and \nocite the rest in a normal (non-modified) authoryear document, I get a disambiguation. –  moewe Apr 9 at 14:40
If I run the OP's MWE I do get "N. Author (2014)" instead of just "Author (2014)", that is the disambiguation I was referring to and was missing in your solution. (I was not talking about the letters added to the year for works by the same author(s) from the same year, that is where my example above gets into trouble.) –  moewe Apr 9 at 15:03
@moewe You are correct, my solution screws up disambiguation. It also seems to screw up the sort order. It appears that \sortlist{entry}{nyt} doesn't actually do the sorting. –  StrongBad Apr 9 at 15:17
@moewe now I see, I am a little slow today. I will have to look into how biber and biblatex do disambiguation. –  StrongBad Apr 9 at 15:21