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:


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):



And here is an example of the chapters:

    %...... here goes the document text

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


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

  • You can try biblatex (check out its documentation for available solutions).
    – Leonid
    Commented Feb 9, 2019 at 20:14
  • 1
    ... 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
    Commented Feb 9, 2019 at 20:33
  • 1
    Possibly related: tex.stackexchange.com/q/305021/35864. See also tex.stackexchange.com/q/353679/35864
    – moewe
    Commented Feb 9, 2019 at 20:40
  • 1
    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
    Commented Feb 10, 2019 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
    Commented Feb 13, 2019 at 6:48

1 Answer 1


I finally managed to find a workaround for this issue and I post it here for future reference.

The key is in the usage of another package, the bibunits package!

What needs to change in the code provided above is to include the following lines in the main file (just above the \begin{document} command):

\defaultbibliography{bibliography} % This is the one and only bibliography file for all chapters
\defaultbibliographystyle{apalike} % This is the style to be used throughout the book

and then, in every subfile (chapter), include everything between \begin-\end{bibunit} as follows:


%.......text with \cite{...} commands

\putbib % this is the command that renders the chapter bibliography here!

Apparently the code lines


are no longer needed (in case we only need a global book compilation), and they are substituted by a single \putbib command.

When the main file is compiled, then a series of bu*.aux files are created that correspond to the auxiliary files for the subfiles (chapters). Then those auxiliary files need to be used as input to bibtex but this might not be so easy, since we are talking about multiple files and in many cases, like when using TeXShop on MacOS, there is no option to run multiple arbitrarily-named auxiliary files. To tackle this I had to create a very simple "engine" with the following code (which practically runs bibtex for every bu*.aux file):

for auxfile in bu*.aux
  echo ---Running bibtex $auxfile 
  bibtex "$auxfile" 

I saved this code as BIBunits.engine and moved it in the Engines folder in the Library of TeXShop (~\Library\TeXShop\Engines) so that the "engine" becomes available within the TeXShop GUI.

After all those steps the issue is resolved. I get a complete book with the bibliography of each chapter where it should be. If everything is done the way presented above here is the pipeline:

  1. create and edit the files
  2. compile the main file
  3. run the BIBunits.engine (bibtex all bu*aux files)
  4. compile the main file twice

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .