gsalzer / subfiles

class and package for multi-file projects in LaTeX
LaTeX Project Public License v1.3c
25 stars 1 forks source link

bibliography and subfolders #29

Closed tobiasBora closed 3 years ago

tobiasBora commented 3 years ago

Hello,

First, thanks for this great package. However, I have issues when using \bibliography and subfolders. Indeed, the documentation says that \bibliography automatically applies the subfix 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:

image

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

\RequirePackage{fix-cm} % Correct scaling of CM font
\documentclass[a4paper,12pt]{memoir}

\usepackage{subfiles}

% Print bibliography at end of each subfile when compiling separately,
% but also in main document when compiling together. To treat subfile and
% main file differently, see https://tex.stackexchange.com/a/358429/116348
\AtEndDocument{%
  \bibliographystyle{alpha}
  \bibliography{biblio}
}

\begin{document}

\chapter{Thanks}
This is my main document.

\subfile{Chapters/01_chapter_intro.tex}

\end{document}

biblio.bib

@article{Mer89_WhatWrongThis,
  title = {What's {{Wrong}} with This {{Pillow}}?},
  author = {Mermin, N. David},
  year = {1989},
  month = apr,
  volume = {42},
  pages = {9--11},
  publisher = {{American Institute of Physics}},
  issn = {0031-9228},
  doi = {10.1063/1.2810963},
  journal = {Physics Today},
  number = {4}
}

Chapters/01_chapter_intro.tex:

\documentclass[../main]{subfiles}

\begin{document}
\chapter{Hello}
Hello \cite{Mer89_WhatWrongThis}.

\end{document}
gsalzer commented 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}
tobiasBora commented 3 years ago

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!

gsalzer commented 3 years ago

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