1

Consider the following main.tex:

\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} 

Here is chapter1.tex:

\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{My Title}
\cite{key}   % the problem
\end{document}

Compiling main.tex with pdflatex bibtex pdflatex pdflatex and all is good. But a primary goal of subfiles is to allow subfile compilation, and I cannot figure out how to compile chapter1.tex in this way with the citation defined.

Can someone please post the exact changes that will allow this (with bibtex)? The "hints" in the subfiles documentation are not enough for me. (I tried \subfix without luck.)

Alan
  • 303
  • 1
    Somehow the contents of your main document and chapter1.tex are identical. In particular chapter1.tex has \subfile{chapter1.tex}, which to me looks like an infinite loop waiting to happen. If you really use \jobname in 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 \jobname may only be here for the MWE). Can you show us the .log and (probably more importantly here) .blg file of the subfile compilation. Do you use a more complex folder structure in your real-world example? – moewe Mar 30 '20 at 06:16
  • @moewe Example fixed. And yes, the use of \jobname is just for a MWE. – Alan Mar 30 '20 at 12:46

1 Answers1

1

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.

moewe
  • 175,683
  • 1
    Related: https://tex.stackexchange.com/q/217517/35864 – moewe Mar 30 '20 at 16:39
  • So ... is the "hint" in the subfiles documentation completely useless?? – Alan Apr 01 '20 at 02:31
  • @Alan For the most part the hint is correct, but not particularly helpful for your use case. I guess the package author mainly tested the package in set-ups where the subfiles contained the \bibliography call (possibly with a package such as bibunits or chapetrbib), where your problem may not a arise. You can always open an issue at https://github.com/gsalzer/subfiles/issues and ask for clarification of the docs or for a more official interface for my implementation of \subfilesbibliography. – moewe Apr 01 '20 at 04:54
  • 1
    Issue opened at https://github.com/gsalzer/subfiles/issues/7 – moewe Apr 03 '20 at 19:17