[Tex/LaTex] bibtex + standalone: How to include bibliographies in the individual docs *and* the main doc

bibliographiesbibtexstandalone

I'm trying to work on individual sections of a paper separately. In order to do this, I need to wrap the text in

\begin{document}
...
\bibliography{bib}
\end{document}

The main file looks similar:

\begin{document}
\include{A}
\include{B}
...
\bibliography{bib}
\end{document}

However, with the bibliography included in the sub-file and the main file, I end up with these errors: Illegal, another \bibstyle command, Illegal, another \bibdata command. I think, had the builds worked, I would also have a duplicate bibliography.

So, the question: How can I keep the individual sub-documents compileable (with bibliography), but also have the "main" document compile with a working bibliography?

Best Answer

Assuming you are using the standalone bundle here then you can use the \ifstandalone switch around the \bibliography{..} command to ignore it if the sub-file is compiled as part of the main document. This switch is set to \iftrue by the standalone class, but to \iffalse by the standalone package in the main document. See the standalone package manual for more details.

% Subfile e.g. "A.tex"
\documentclass{standalone}
\begin{document}
...
\ifstandalone
\bibliography{bib}
\fi
\end{document}
% main document
\documentclass{book}
\usepackage{standalone}
\begin{document}
\include{A}
\include{B}
...
\bibliography{bib}
\end{document}
Related Question