I think what you want is an easy way to select which part(s) should be compiled in the master file.
The following supposes that you have two chapters in the book, and each of them has three sections as you described.
STEP 1
This is the master file:
% master file: main.tex
\documentclass{book}
\includeonly{chapter1, chapter2}
\begin{document}
\include{chapter1}
\include{chapter2}
\end{document}
Chapter 1:
% chapter 1, main file
\chapter{This is chapter one}
\input{note1}
\input{exercise1}
\input{solution1}
Chapter 2:
% chapter 2, main file
\chapter{This is chapter two}
\input{note2}
\input{exercise2}
\input{solution2}
In the six subsubfiles, there are some dummy text.
STEP 2
Compile the master file, and then, select which chapter you want to compile in \includeonly{}
(separate eachother by comma).
STEP 3
Compile the master file again.
You will find that only the chapter(s) you selected was compiled, and the page numbers, chapter and section numbers remains correct.
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:
- create and edit the files
- compile the main file
- run the
BIBunits.engine
(bibtex
all bu*aux
files)
- compile the main file twice
Best Answer
This is not my solution, but I thought I would combine two other solutions that I found on the site.
Bibliographies when using subfiles and subfiles inside a subfile using relative paths
As Before
in the
./rootdoc.tex
fileand in the
./tex/childdoc.tex
filenow the correct
.bib
file is referenced and when child is compiled the bibliography does not appear twice when the root document is compiled.