# Multiple subfiles in folders sharing the same root bibliography

I created a book project (multilingual XeLaTeX) following the modular approach from https://en.wikibooks.org/wiki/LaTeX/Modular_Documents and particularly @ paragraphs "Subfiles" (for the usage of the commands \onlyinsubfile and \notinsubfile) and @ "Subfiles: master + slave in different subdirectories" (for the correct paths).

I need to be able to compile both chapters separately as well as the whole book. I need to have the references per chapter and not at the end of the book.

I have the following file structure:

root folder: with book.tex, bibliography.bib
/figures folder with all figures
/chapterX folder: for each chapterX.tex

Each chapter separately compiles great!

But when I try to compile the whole book I get the full set of references at the end of each chapter. I imagine this is due to the fact that when compiling the whole thing, all references are being written to the auxiliary files and when the chapter bibliographies are being compiled one-by-one they contain the whole set of references. I tried cutting the bibliography into pieces, having ie. bibliography1.bib inside chapter1 with only the references for chapter1 but the result is most awkward: all chapter bibliographies become the reduced bibliography1.bib!

In my preamble I use:

\usepackage[semicolon,round,sort&compress,sectionbib]{natbib}
\usepackage{chapterbib}


Is there a work around?

## -----------------------------------------------

Since a couple of people where interested, I am including a minimal piece of code to be more specific. Nevertheless I think the issue might be structural and deep and no-matter what code I provide the issue remains...I hope I am wrong.

This is the main latex file (book.tex):

\documentclass[11pt,b5paper,twoside,openright]{book}
\usepackage[semicolon,round,sort&compress,sectionbib]{natbib}
\usepackage[sectionbib]{chapterbib}
\usepackage{apalike}
\usepackage[toc,page,titletoc]{appendix}
\bibliographystyle{apalike}
\usepackage{subfiles}
\newcommand{\onlyinsubfile}[1]{#1}
\newcommand{\notinsubfile}[1]{}

\begin{document}
\renewcommand{\onlyinsubfile}[1]{}
\renewcommand{\notinsubfile}[1]{#1}
\subfile{chapter0/chapter0}
\subfile{chapter1/chapter1}
\subfile{chapter2/chapter2}
\subfile{chapter3/chapter3}
\begin{appendices}
\subfile{appendixI/appendixI}
\subfile{appendixII/appendixII}
\end{appendices}
\end{document}


And here is an example of the chapters:

\makeatletter
\def\input@path{{../}}
\makeatother
\documentclass[../book.tex]{subfiles}
\begin{document}
%...... here goes the document text
\onlyinsubfile{\bibliography{../bibliography.bib}}
\notinsubfile{\bibliography{./bibliography.bib}}
\end{document}


Apparently, the preamble in this chapter requires a couple of commands to specify the relative path and the main latex file. The commands

\onlyinsubfile
\notinsubfile


are as specified in the reference I have provided from wikibooks.

• You can try biblatex (check out its documentation for available solutions). – Leonid Feb 9 at 20:14
• ... though biblatex also has had its fair share of troubles with subfiles. So I'm not sure if everything will go smoothly at the first attempt. Anyway, you could increase your chances of getting an answer if you could share an MWE (tex.meta.stackexchange.com/q/228/35864) or MWEB (tex.meta.stackexchange.com/q/4407/35864) of your setup. The general issue is understandable without an MWE, but some details of your chapterbib usage may be important and it is always a nice gesture to give people something to start from instead of having them come up with stuff you have already. – moewe Feb 9 at 20:33
• Possibly related: tex.stackexchange.com/q/305021/35864. See also tex.stackexchange.com/q/353679/35864 – moewe Feb 9 at 20:40
• I tried to include a minimal piece of code to be more specific, although I feel the issue is structural and possibly cannot be addressed under the assumptions and the packages that I am using... – sepdek Feb 10 at 9:11
• With new files, I still get the error Illegal, another \bibdata command---line 8 of file book.aux : \bibdata : {./bibliography.bib} when running bibtex. Have you looked at links given by @moewe ? – Joce Feb 13 at 6:48