[Tex/LaTex] How to use bibtex with subfile

bibtexsubfiles

I am a newbie, and I have been looking for the solution for a while. But if I use subfile for separate files, how can I do citations in the subfiles that refer to the global bibliography? Like if I insert a bibliography in the main tex file, that will include every citation I have in every subfiles?

Thank you very much!

Best Answer

Citations in subfiles that refer to a global bibliography that is set up in the main file work exactly as if there was no subfile.

The following is the file main.tex which does not contain any \cite commands, but prints the bibliography.

\documentclass{report}
\usepackage{filecontents}
\usepackage{subfiles}
\begin{filecontents}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
}
\end{filecontents}

\begin{document}
\subfile{chapter1.tex}
\bibliographystyle{unsrt}
\bibliography{\jobname}
\end{document} 

The following is the file chapter1.tex which contains the only \cite command.

\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{title}
\cite{key}
\end{document}

To get the desired result, you can compile main.tex using PDFLaTeX, BibTeX, PDFLaTeX, PDFLaTeX.


Alternative approach using \include instead of the subfiles package:

The following is a MWE that uses \include{filename} commands instead of the subfiles package:

main.tex:

\documentclass{report}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
}
\end{filecontents}
\begin{document}
some test
\ref{chapter}

\include{chapter1}
\bibliographystyle{unsrt}
\bibliography{\jobname}
\end{document}

chapter1.tex:

\chapter{title}
\label{chapter}
some text
\cite{key}

Using this approach, TeXMaker auto completes \cite and \ref commands in both files equally.

Please note the differences in the chapter files in comparison to the subfiles approach. And please also keep in mind that \include{filename} does a \clearpage before and after \include{filename}.

Further information: When should I use \input vs. \include?

Related Question