6

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.

sepdek
  • 71
  • You can try biblatex (check out its documentation for available solutions). – Leonid Feb 09 '19 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 (https://tex.meta.stackexchange.com/q/228/35864) or MWEB (https://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 09 '19 at 20:33
  • 1
    Possibly related: https://tex.stackexchange.com/q/305021/35864. See also https://tex.stackexchange.com/q/353679/35864 – moewe Feb 09 '19 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 '19 at 09: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 '19 at 06:48
  • @Joce I cannot see any other relevant commands to include for the error you are getting. Did you try to erase all auxiliary files before compiling the new code? Regarding the links by moewe, yes I looked them up (and also a lot more) but they are all connected with slightly different problems. – sepdek Feb 13 '19 at 07:14
  • Can you post the content of book.aux with these MWE and also the output of the bibtex command? Btw, I believe solutions in links by moewe very relevant to your question. – Joce Feb 13 '19 at 08:12
  • @Jose: this is why the issue here is different from those suggested by moewe: --- (a) in the first case, it is stated that "I can build the main document (thesis.tex) with the subfiles, but no references are included. Trying to then build the main document with bibtex pulls up an error..." ----- I think in my case I am way past these situations. --- (b) in the second case, multiple bibliographies are being used which results in an entirely different problem! --- Regarding my book.aux file, what do you need from it specifically? It is a 100K huge text files! I can't just post it here! – sepdek Feb 13 '19 at 14:41
  • You are telling me that you are getting a different output than I do when running the above MW example. In order to understand, the corresponding book.aux file would be helpful. Concerning the other answers, it is not because the questions of the OPs state a different outcome with their code that the same solution is not helpful. – Joce Feb 13 '19 at 16:56

1 Answers1

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

\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
sepdek
  • 71