If we run bibtex chapter1 (after having run pdflatex chapter1) we get
This is BibTeX, Version 0.99d (MiKTeX 2.9.7380 64-bit)
The top-level auxiliary file: chapter1.aux
I found no \bibdata command---while reading file chapter1.aux
I found no \bibstyle command---while reading file chapter1.aux
so BibTeX complains about not knowing which style to take and which .bib file to access. Essentially that is because chapter1.tex has neither a \bibliographystyle
call to set the bibliography style, nor a \bibliography call to tell BibTeX which .bib file to use. The subfile can read the preamble of its parent, but not the document body, so it never gets to see the \bibliographystyle{unsrt} and \bibliography{\jobname} in main.tex.
We need a way to tell BibTeX about the style and the .bib file.
For the bibliography style that is simple, because \bibliographystyle may be used in the preamble, so you could just make your main.tex read
\documentclass{report}
\usepackage{subfiles}
\bibliographystyle{unsrt}
\begin{document}
\subfile{chapter1.tex}
\bibliography{mybibfile}
\end{document}
But that still leaves chapter1.tex unaware of \bibliography. Unfortunately, it is not possible to simply move \bibliography into the preamble, because (with BibTeX) \bibliography must be used at the place where the bibliography is printed.
You need a \bibliography call in chapter1.tex if you want to compile the file separately with working citations. But just adding \bibliography{mybibfile} to chapter1.tex will lead to two bibliographies (and/or an error) in main.tex. Ideally, we'd find a way to check if we are compiling the subfile chapter1.tex on its own or in the context of the full file main.tex. I didn't find an official check for that in the documentation, but think it would be a useful addition, so I had to roll my own test that replicates \@ifclassloaded: If chapter1.tex is compiled on its own, the class will be subfiles, if it is compiled in the context of main.tex the class is different.
So here we are: \subfilesbibliography creates a bibliography when the file is compiled on its own, but not when it is compiled in the main document.
main.tex
\documentclass{report}
\usepackage{subfiles}
\bibliographystyle{unsrt}
\makeatletter
% curse you, \@onlypreamble\@ifclassloaded
\newcommand*{\subfilesbibliography}[1]{%
\expandafter\ifx\csname ver@subfiles.cls\endcsname\relax
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
{\bibliography{#1}}
{}%
}
\makeatother
\begin{document}
\subfile{chapter1.tex}
\bibliography{mybibfile}
\end{document}
chapter1.tex
\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{My Title}
\cite{key} % the problem
\subfilesbibliography{mybibfile}
\end{document}
Note that I used a fixed file name instead of \jobname, because the variable nature of \jobname would make things much more complicated.
chapter1.texare identical. In particularchapter1.texhas\subfile{chapter1.tex}, which to me looks like an infinite loop waiting to happen. If you really use\jobnamein your actual document that could be an issue as that command may or may not be different for compilation of the main file and the subfile (but of course that\jobnamemay only be here for the MWE). Can you show us the.logand (probably more importantly here).blgfile of the subfile compilation. Do you use a more complex folder structure in your real-world example? – moewe Mar 30 '20 at 06:16\jobnameis just for a MWE. – Alan Mar 30 '20 at 12:46