Closed tobiasBora closed 3 years ago
Thanks for taking the time to write a self-contained report. Below you find a fix, then an explanation why it doesn't work the way you try, and finally the complete code.
The fix: Don't use \AtEndDocument
, but add the line \bibliography{biblio}
at the end of the main file and
\ifSubfilesClassLoaded{%
\bibliography{../biblio}%
}{}
at the end of the subfile.
Why \AtEndDocument
doesn't work: The effect of this command is that \bibliography{biblio}
is executed before the regular \end{document}
stuff, exactly as if the line appeared literally there. There is no way for the \bibliography
(or the \subfix
) command to remember that the command was added to the 'Do-At-End-Of-Document' list in the main file, as it isn't executed there. The philosophy of the subfiles
package is that files mentioned in a subfile are searched relative to the subfile. This means that if you have \bibliography{biblio}
in a subfile in another directory, then biblio
refers to a file in the other directory. This mechanism is usually what you want: If you have \bibliography{../biblio}
(or similarly, something like \includegraphics{../ImageInMain}
) in the subfile, then it will refer to the correct file no matter whether you compile the subfile on its own or as part of the main file. A further complication of \bibliography{file}
is that file
is not processed by TeX but by an external program, BibTeX
, so it also wouldn't help to add more directories to the \input
search path, as file
isn't loaded by \input
.
Working code:
% main.tex
\documentclass{report}
\bibliographystyle{alpha}
\usepackage{subfiles}
\begin{document}
\chapter{Thanks}
This is my main document.
\subfile{Chapters/01_chapter_intro}
\bibliography{biblio}
\end{document}
% Chapters/01_chapter_intro.tex
\documentclass[../main]{subfiles}
\begin{document}
\chapter{Hello}
Hello \cite{Mer89_WhatWrongThis}.
\ifSubfilesClassLoaded{%
\bibliography{../biblio}%
}{}
\end{document}
Whoa, thank you so much for such a detailled answer and explanation, everything is much clearer now (maybe some of the explaination, notably on the fact that path are relative to the current file, should go in the documentation?).
I just wanted to avoid to manually modify each chapter one by one if for instance I do a change in the bibliogryphy style, but I guess I'll either create one file that I will input in all chapters, or combine AtEndDocument with your command, and put all chapters at the same level. Thanks a lot!
Maybe you should try another way to do the bibliographies. You use the ancient method with bibtex, which has the disadvantage that you have to specify the bib-file at each place where you want a list of references. With biblatex/biber, you specify the bib-files in one place (e.g. in the preamble of main), and use a separate command to generate the bibliographies.
See also the tests
folder, in particular the tests biblatex
, bibunits
and chapterbib
Hello,
First, thanks for this great package. However, I have issues when using
\bibliography
and subfolders. Indeed, the documentation says that\bibliography
automatically applies thesubfix
fix to find automatically the proper bibliography path no matter in which subfolder I am. However, it seems that it does not work, since when compiling the chapters, the references are not found:A first fix is to use
\ifSubfilesClassLoaded
, but it only works if all chapters are in the same subfolder, which may not be the case in practice.MWE:
main.tex
biblio.bib
Chapters/01_chapter_intro.tex
: