5

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
  • 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 Feb 9 at 20:33
  • 1
    Possibly related: tex.stackexchange.com/q/305021/35864. See also tex.stackexchange.com/q/353679/35864 – moewe Feb 9 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 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
0

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

\usepackage{bibunits}
\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:

\begin{document}
\begin{bibunit}

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

\putbib % this is the command that renders the chapter bibliography here!
\end{bibunit}
\end{document}

Apparently the code lines

\onlyinsubfile{\bibliography{../bibliography.bib}}
\notinsubfile{\bibliography{./bibliography.bib}}

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

#!/bin/sh 
for auxfile in bu*.aux
do
  echo ---Running bibtex $auxfile 
  bibtex "$auxfile" 
done 

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

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.