0

I'm using subfiles to split my document into multiple files, one per chapter. It works great, except for bibliography: if I compile the main document, the items are found, but if I compile a chapter alone, no bibliography item are found. The documention of subfiles suggests that is should "just work" :

Some commands already apply the fix on the fly. At the moment these are the standard LaTeX command \bibliography and \graphicspath.

Any idea what's wrong?

Main document:

enter image description here

Chapter:

enter image description here

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},
  file = {/home/leo/Zotero/storage/WV73TT5N/1.html},
  journal = {Physics Today},
  number = {4}
}

Chapters/01_chapter_intro.tex:

\documentclass[../main]{subfiles}

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

\end{document}
2

Using

\AtEndDocument{%
    \bibliographystyle{alpha}
    \bibliography{biblio}
}

will make the subfiles missing the biblio.bib file, because for them the correct path is ../biblio. With bibtex the command \bibliography{biblio} points to a single file, you can not add multiple search paths.

(Using instead \bibliography{../biblio} will succeed on chapters but then it will fail when compiling main.tex)

One way to avoid this is to detect if you are in the preamble or in the text of the document.

This is done with the command \insubfile (from \renewcommand only in subordinated file with subfiles package)

Or see my update at the end!

While compiling the subfiles \bibliography{../References/biblio} will be executed; instead these command(s) will be ignored compiling main.tex, but \bibliography{./References/biblio} will be executed at the end of the document.

I choose a slightly more complex directory arrangement to test the solution. I like to have all the files belonging to a project under the same roof, so to speak.

T

After compiling each chapter separately, these are the individual results:

01_chapter_intro.tex

S1

02_chapter_one.tex

S2

For main. tex you get

M

File MyProject/main.tex

%% MyProject/main.tex

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

\usepackage{subfiles}

%%      https://tex.stackexchange.com/a/515672/161015
%%      https://tex.stackexchange.com/a/16298/161015
\makeatletter
\newcommand{\insubfile}[1]{\ifx\@onlypreamble\@notprerr\else#1\fi}
\makeatother

\bibliographystyle{alpha}

\begin{document}
    
    \chapter{Thanks}
    This is my main document.   Starts  with \cite{Mer89_WhatWrongThis}.
    
    \subfile{./Chapters/01_chapter_intro}
    
    \subfile{./Chapters/02_chapter_one}     

    \bibliography{./References/biblio}
    
\end{document}

File MyProject/Chapters/01_chapter_intro.tex

%% MyProject/Chapters/01_chapter_intro.tex

\documentclass[../main]{subfiles}
\insubfile{\AtEndDocument{\bibliography{../References/biblio}}}

\begin{document}
    \chapter{Hello}
    And then \cite{einstein}.   
\end{document}

File MyProject/Chapters/02_chapter_one.tex

%% MyProject/Chapters/02_chapter_one.tex

\documentclass[../main]{subfiles}
\insubfile{\AtEndDocument{\bibliography{../References/biblio}}}

\begin{document}
    \chapter{One}
    Only \cite{dirac}.  
\end{document}

File MyProject/References/biblio.bib

%%% MyProject/References/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},
    file = {/home/leo/Zotero/storage/WV73TT5N/1.html},
    journal = {Physics Today},
    number = {4}
}

  @article{einstein,
    author={Albert Einstein},
    title={Zur Elektrodynamik bewegter Korper},
    journal={Annalen der Physik},
    volume={322},
    number={10},
    pages={891--921},
    year={1905},
    DOI ={http://dx.doi.org/10.1002/andp.19053221004}
}

@book{dirac,
    title={The Principles of Quantum Mechanics},
    author={Paul Adrien Maurice Dirac},
    isbn={9780198520115},
    series={International series of monographs on physics},
    year={1981},
    publisher={Clarendon Press},
    keywords = {physics}
}

UPDATE (well ...)

Reading the package subfile document I found out that a new command was added to the class in October 2020.

3.3 Conditional execution of commands

The command \ifSubfilesClassLoaded is useful to execute commands conditionally, depending on whether the main file is typeset or a subfile.

\ifSubfilesClassLoaded{% then branch
    . . .  commands executed when the subfile is typeset . . .
}{% else branch.
 . .  commands executed when the main file is typeset . . .
}

As an example, this can be used to add the bibliography to the main document or to the subdocument, whichever is typeset:

2
  • Thanks for your answer (+1). I was aware of ifSubfilesClassLoaded, but I wanted to avoid to hardcode the path depending on whether I'm in the subfile or in the main file, because this solution will not work if my chapters are not all in the same subfolders. I thought that subfix was supposed to solve this issue, and that bibliography was automatically applying the fix on the fly... but apparently it's not working. Your proposed fix is good enough for my needs right now, but anyway I filled a bug there github.com/gsalzer/subfiles/issues/29
    – tobiasBora
    Jul 12 at 7:43
  • @tobiasBora The solution will work, even if your chapter are in different directories, provided that the references are in a upper level directory, able to be reached by relative path movements like ../../../References in the same way for all chapters. Jul 12 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.