5

Suppose I have a cats.bib and hats.bib which I want to use in an article on cats in hats. There already is an existing paper about that (well, sort of), which I have in both files.

Now, I do not want to merge these files (unlike in this question and this one). I am willing to allow whatever tool I use to ignore the possibility of differences between the entries and just pick whichever one it likes (not even consistently; I mean, I'm willing to guarantree they're really copies of the same entry). Under these conditions, what can I do to be able to use both bibliographies in my LaTeX document without getting any errors?

If you suggest doing something temporarily merging them just for the purpose of compiling the document, please sugget something which would integrate into, say, latexmk, so that I don't have to repeatedly do it manually, and hopefully also that whatever tool does this gets the bibliography names from the .tex file itself rather than apriori.

einpoklum
  • 12,311

2 Answers2

10

You can use as many .bib files as you want. Duplicate entries will result in a BibTeX error which has no consequence and keeps only the first found entry.

\begin{filecontents*}{\jobname-cats.bib}
@article{unique-cats,
  author={A. Uthor},
  title={Title},
  journal={Journal},
  year=2016,
}
@article{duplicate,
  author={W. Riter},
  title={Title},
  journal={Journal},
  year=2016,
}
\end{filecontents*}
\begin{filecontents*}{\jobname-hats.bib}
@article{unique-hats,
  author={P. Laywright},
  title={Title},
  journal={Journal},
  year=2016,
}
@article{duplicate,
  author={W. Riter},
  title={Title},
  journal={Journal},
  year=2016,
}
\end{filecontents*}

\documentclass{article}

\begin{document}

\title{Cats in hats}
\author{Einpoklum}
\maketitle

We want to cite \cite{unique-cats}, but also \cite{unique-hats}.

There is a duplicate \cite{duplicate}.

\bibliographystyle{plain}
\bibliography{\jobname-cats,\jobname-hats}

\end{document}

Here's the warning by BibTeX:

This is BibTeX, Version 0.99d (TeX Live 2016)
The top-level auxiliary file: einmulti.aux
The style file: plain.bst
Database file #1: einmulti-cats.bib
Database file #2: einmulti-hats.bib
Repeated entry---line 7 of file einmulti-hats.bib
 : @article{duplicate
 :                   ,
I'm skipping whatever remains of this entry
(There was 1 error message)

enter image description here

egreg
  • 1,121,712
  • Re: "has no consequence and keeps only the first found entry": actually, for a refsection drawing only from the second bib file there are consequences: references that are also present in the first bib file won't be found in the refsection, since they've been skipped. – stafusa Sep 28 '21 at 08:45
1

Bib files are just plain text, and there's no equivalent to \end{document} so you can just concatenate them (as you accept the responsiility for duplicate entries).

On linux with --shell-escape you could do something like

\documentclass{article}
\immediate\write18 {cat nice_papers*.bib > \jobname.bib}
\addbibresource{\jobname.bib}
\begin{document}

On windows it would be similar: Instead of cat... you would use copy /y nice_papers*.bib \jobname.bib. This assumes biblatex, but the change for bibtex is trivial (\bibliography{\jobname}).

This is should be compatible with latexmk as it uses no external tools.

One thing to watch out for is that your source bib files should end with one or more newlines so you don't end up with a garbage line at the merge point (this is especially true if you have comments in your bibfile)

Chris H
  • 8,705
  • This was intended to be a comment originally, but the *nix and windows solutions (and bibtex/biblatex) ended up close enough that I made it a general answer – Chris H Feb 03 '17 at 14:11
  • (1) Where will the \write18 look for the files? Same place as an \addbibresource or a \bibliography command? (2) You are suggesting/assuming I'll use the biblatex package? – einpoklum Feb 03 '17 at 14:38
  • @einpoklum That wildcard-free copy command on windows would write cats.bib over the top of hats.bib redirecting the status to \jobname.bib. But the redirect in mine was wrong so I've corrected that – Chris H Feb 03 '17 at 16:16
  • If you want to use bibtex, the susbtitution is is just to call \bibliogrpahy{\jobname} (not.bib). – Chris H Feb 03 '17 at 16:19