[Tex/LaTex] Multiple LaTeX files using the same bibliography

bibliographiesbibtex

I have multiple different LaTeX files each which draw their bibliography from the same .bib file. When they compile I want them to share one bibliography file that is a separate PDF file. For example assume I have

one.tex
two.tex
three.tex
bibliography.bib

I want to create

one.pdf
two.pdf
three.pdf
bibliography.pdf

Where neither one.pdf, two.pdf nor three.pdf have their own bibliography in them, but instead all reference the same bibliography.pdf file. Is this possible to do, and if so how?

Best Answer

Write a bibliography.tex file containing

\documentclass{article}
\begin{document}
\nocite{*}
\bibliographystyle{plain}
\bibliography{bibliography}
\end{document}

and compile it (LaTeX, BibTeX, LaTeX, LaTeX). Then one.tex and the others can be

\documentclass{article}
\usepackage{xcite}
\externalcitedocument{bibliography}

\begin{document}
...
\end{document}

Note that the xcite package was born from a question here on TeX.SX: Use bibliography numbers and citation from other file

Instead of saying \nocite{*} in bibliography.tex you can gather the requested citations from your files by a simple shell one-liner

grep -h citation one.aux two.aux three.aux | sed /citation/s//nocite/ > cites.tex

and say \input{cites} in bibliography.tex instead of \nocite{*}.

A bit of explanation

When LaTeX finds a \cite command, it writes it in the .aux file for possible later processing with BibTeX. So \cite{xyz} will produce a line in the .aux file of the form

\citation{xyz}

In the presented case you have a big .bib file and want that only actually cited entries are extracted from it. But the entries are scattered among three files to be compiled independently and possibly not each of them will cite all the requested entries.

So the strategy is to delay bibliography typesetting until all the citations are present in the three .aux files. They will show the typical ?? in the text, but this is unimportant.

The bibliography.tex file will be of the form

\documentclass{article}
\begin{document}
\input{cites}
\bibliographystyle{plain}
\bibliography{bibliography}
\end{document}

and we will populate a cites.tex file with the necessary BibTeX keys. The one-liner

grep -h citation one.aux two.aux three.aux | sed /citation/s//nocite/ > cites.tex

will find all \citation{...} lines in the three .aux files; they will be filtered by sed that will change \citation into \nocite and the output will be written in cites.tex that will consist of lines of the form

\nocite{abc}
\nocite{def}
...

with all the cited entry keys. Now we can run LaTeX on bibliography.tex, then BibTeX and then LaTeX again. This will produce the correct bibliography.aux file that LaTeX will read (via the xcite package) when typesetting one.tex, two.tex and three.tex again.