# Multiple LaTeX files using the same bibliography

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?

-

## migrated from stackoverflow.comDec 2 '11 at 0:58

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Torbjørn T. Dec 2 '11 at 1:06

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.

-
This is definitely the simplest solution, and it only requires making a separate .bib file containing only the citations used by the actual documents. But for those of us (including Jon) who have one big mega .bib file it doesn't really work. –  Alan Munn Dec 2 '11 at 18:43
I'm confused by your shell one liner, what are you trying to accomplish? –  Jon Dec 2 '11 at 20:50
@Jon The one liner is in response to my comment. (And your comment about having a global .bib file). It strips out the citations from each of the subdocuments and puts them into a file so that you can use a global bib file. –  Alan Munn Dec 3 '11 at 4:29

Let's assume the following:

• one.tex, two.tex and three.tex have the following structure:

\documentclass{article}
% preamble content
\begin{document}
% document content
\bibliography{bibliography}% expects file "bibliography.bib"
\end{document}

• You are using bibtex

Here are the steps you need to do in order to obtain your required output

1. pdflatex one
2. bibtex one
3. pdflatex one
4. pdflatex one
5. Comment out the line \bibligoraphy{bibliography}
6. pdflatex one
7. Repeat steps 1-6 for two.tex and three.tex
8. Construct bibliography.tex having

\documentclass{article}
% preamble content
\pagestyle{empty}% If needed
\begin{document}
\nocite{*}% Include all references in bibliography.bib
\bibliography{bibliography}% expects file "bibliography.bib"
\end{document}

9. Repeat steps 1-3 for bibliography.tex

Step 5 removes only the printing of the bibliography in the documents, while still keeping the references in place. During this step, the .aux file is overwritten, but the original values contained within are still used/available.

Step 8 can vary greatly, depending on the typesetting output of your bibliography.pdf file. For example, if you want page numbers, or headers/footers. I've included \pagestyle{empty} assuming that you want no header or footer.

-
When I run this I get no output, bibliography.pdf just says References at the top and then has no content. What could be causing this? –  Jon Dec 2 '11 at 17:58
So it worked for one.tex, two.tex and three.tex? Did you do pdflatex, bibtex and then pdflatex for bibliography.tex? –  Werner Dec 2 '11 at 18:00
Right now I'm only testing it with one.tex just to make sure that it works. I get one.tex without a bibliography like I want, but no references. Yes I am doing those steps, here is a copy of my bibliography.text –  Jon Dec 2 '11 at 18:03
\documentclass[11pt]{article} \usepackage{helvet} \renewcommand{\familydefault}{\sfdefault} \usepackage[top=1.27cm, bottom=1.27cm, left=1.27cm, right=1.27cm]{geometry} \pagestyle{empty} \begin{document} \bibliographystyle{plain} \bibliography{bibliography} \end{document} –  Jon Dec 2 '11 at 18:03
Just to be clear I ran > pdflatex bibliography > bibtex bibliography > pdflatex bibliography –  Jon Dec 2 '11 at 18:09

If the .bib file will only be used for those three .tex files, and you're willing to use biblatex, it seems pretty easy to do.

For the three .tex files, cite the bibkeys from your .bib file as normal, but do not include the \printbibliography command in the .tex files. Then, in a fourth file, you just need to use the \nocite{*} and the \printbibliography command for it to print every entry from the bibfile.

Thus, the skeleton of your first three files would be:

\documentclass{<whatever>}
\usepackage[<options>]{biblatex}
\begin{document}
\cite{<key1>}% etc., etc.
\end{document}


while your master bibliography files will be:

\documentclass{<whatever>}
\usepackage[<options>]{biblatex}
\begin{document}
\nocite{*}
\printbibliography
\end{document}


The essential point: this .bib file must only contain entries that are found in your three other .tex files! (Easily extracted from a masterbibliography.bib file through tools like bibtool, etc.

-
The problem is that we have one global .bib file that has many references that I do not want to include. –  Jon Dec 2 '11 at 18:13
@Jon --- that is precisely the point of bibtool: it extracts all citations given in the .aux file and prints them to a new file (usage: bibtool -x masterfile.aux -o newbibfile.bib). It is therefore quite easy to create a new .bib file from any number of 'master' bibliography files. –  jon Dec 2 '11 at 20:02

do all the .tex files reference exactly the same \cites? (this point isn't clear in the question.)

if they do, then the other answers should work.

if they don't, the only way i can see to get a common bibliography is, in each .tex file, to include \nocite{<key>} for each entry that should be included, or, if the .bib file includes only the desired items, then \nocite{*} can be used instead.

going one step further, you could create a single .tex file containing only the \nocite instructions, and \input it to the other three .tex files.

this doesn't give you a separate pdf file, but it does give you the same bibliography content for all three tex jobs.

-