sepdek June 20, 2019
LaTeX logo

I was recently editing the second edition of my book on 3D digitisation using LaTeX and stumbled on an unexpected obstacle. The book consists of a number of chapters thus it is more convenient to edit them separately. In addition, I wanted to have bibliography at the end of each chapter, so that each chapter can be circulated independently. But still, I wanted all the bibliography references to be in the same bib file. Apparently, I needed to be able to compile the chapters separately and the book as a whole. Although this might seem trivial, it is not! And as it turned out after posting the problem on tex.stackexchange there was no answer…So I had to work it through and eventually found a solution, although not so elegant as it should be. The complete thread on tex.stackexchange can be found here.

In order to make it happen, I created the following file structure:

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

In the main (master) TeX file (book.tex) I have something like the following minimal code (for a typical double-sided B5 paper size, 11 points characters):

% define the basic document properties
\documentclass[11pt,b5paper,twoside,openright]{book}

% load the natbib library
\usepackage[semicolon,round,sort&compress,sectionbib]{natbib}

\usepackage[sectionbib]{chapterbib}      % load the chapterbib library
\usepackage{apalike}                     % load the APA style for the bibliography
\usepackage[toc,page,titletoc]{appendix} % settings for Appendices
\bibliographystyle{apalike}              % set the bibliography style to APA
\usepackage{subfiles}                    % load the subfiles library

\usepackage{bibunits}                    % load the bibunits library
\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

\begin{document}
    \subfile{chapter0/chapter0}
    \subfile{chapter1/chapter1}
    \subfile{chapter2/chapter2}
    \subfile{chapter3/chapter3}
    % ......... more chapters
    \begin{appendices}
        \subfile{appendixI/appendixI}
        \subfile{appendixII/appendixII}
        % ......... more appendices
    \end{appendices}
\end{document}

Then each of the chapters has the following minimal form:

\makeatletter
\def\input@path{{../}}
\makeatother
\documentclass[../book.tex]{subfiles}
\begin{document}
\begin{bibunit}

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

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

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 scenario runs smoothly and 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 overall compilation 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

Discussion

comments

Leave a comment.

Your email address will not be published. Required fields are marked*

This site uses Akismet to reduce spam. Learn how your comment data is processed.