4

Introduction

I am writing a report consisting of several assignments for one of my university courses. My OS is Windows and I use TeXstudio as editor. I am using the subfiles package as I want to have separate files for each assignment, being able to compile each file independently and finally assemble everything in one unique document that I have to deliver. I am using a custom class derived from the report class so that each assignment constitutes a chapter of the document and thus each subfile contains a single chapter.

Problem

I would like each chapter to have its own bibliography (and no global bibliography), included in the corresponding subfile. However this has become a nightmare, since I have tried different solutions with little success. A complication of my situation is given by the fact that I use the minted package to display some code. Of course I need to invoke the -shell-escape flag in TeXstudio, but this seems to interfere with the generation of .bbl file when compiling the subfile.

An explanation of my attempts follows and at the end of the question you can find a MWE including all attempted approaches.

Attempted solutions

  • natbib+chapterbib approach: inspired by this answer (where however \include is used instead of \subfile). If the minted package is not used, compiling subfile works (even if weird stuff appears in the bibliography, probably due to the style chosen) but when the main file is compiled the entire bibliography is repeated for each chapter, whereas only the chapter-related bibliography should appear.

chapter1.tex output:

enter image description here

main.tex output:

enter image description here

If the minted package is used, no .bbl file is generated when both chapter1.tex and main.tex are compiled.

chapter1.tex output:

enter image description here

  • natbib+bibunits approach: inspired by this answer, the output when compiling the subfile is the same whether minted is in use or not. However, if minted is used, no .bbl file at all is generated, instead if minted is not used the chapter1.bbl file is generated, but apparently a bu1.bbl file is missing.

chapter1.tex output:

enter image description here

  • biblatex approach: inspired by this answer (where however there is no use of subfiles package). This attempt makes use of the refsection feature of biblatex. The output when compiling the subfile is the same whether minted is in use or not: the bibliography items are recognized but the bibliography itself is not printed. It is quite curious that if minted is used, no .bbl file at all is generated, instead if minted is not used the chapter1.bbl file appears in the folder.

chapter1.tex output:

enter image description here

Project structure and MWE

All approaches are reported in the codes listed, you can switch from one approach to the other by toggling the corresponding lines in both the main and the subfile. You will notice that I have both a bibgraf.bib file at the level of main.tex and bibgraf1.bib, bibgraf2.bib files at the level of the corresponding chapters. The former includes all bibliography items, while the latters only the chapter-related items. I do not have any preference regarding the approaches (packages to use, one main root .bib file or multiple .bib files for each chapter, etc.) I would just like to have each chapter with its corresponding bibliography within the subfile.

-project
    -main.tex
    -bibgraf.bib
    -chapter1
        -chapter1.tex
        -bibgraf1.bib
    -chapter2
        -chapter2.tex
        -bibgraf2.bib

main.tex

% Definition of main command to access subfiles input both from master and slave files
\providecommand{\main}{.}

% Document class
\documentclass{report}

\usepackage[english]{babel} % management of culturally-determined typographical (and other) rules for the document language

% Package to generate code listing
\usepackage[newfloat]{minted}
% newfloat: the float package is used to create the listing environment

% Subfiles package
\usepackage{subfiles}

% Bibliography packages
%---- Natbib + chapterbib approach -----
%\usepackage[sectionbib,super,sort]{natbib}
%\usepackage{chapterbib}
%---------------------------------------
%---- Natbib + bibunit approach -----
%\usepackage[sectionbib]{natbib}
%\usepackage{bibunits}
%\defaultbibliographystyle{apsrev4-1}
%------------------------------------
%---- Biblatex approach -----
\usepackage{biblatex}
\addbibresource{bibgraf.bib}
%----------------------------

% Change chapter name
\addto\captionsenglish{\renewcommand{\chaptername}{Assignment}}

\begin{document}

    This is something from the main

    % First chapter
    \subfile{chapter1/chapter1}

    % Second chapter
    \subfile{chapter2/chapter2}

\end{document}

bibgraf.bib

@CONTROL{REVTEX41Control}
@CONTROL{apsrev41Control,author="00",editor="1",pages="1",title="0",year="0"}

@article{Kulfan2008,
    author = {Kulfan, B. M.},
    title = {Universal Parametric Geometry Representation Method},
    journal = {Journal of Aircraft},
    volume = {45},
    number = {1},
    pages = {142-158},
    year = {2008},
    doi = {10.2514/1.29958},
    URL = { 
    https://doi.org/10.2514/1.29958
    },
    eprint = { 
    https://doi.org/10.2514/1.29958
    }
}

@book{anderson,
    title={Fundamental of Aerodynamics},
    author={Anderson, Jr., J. D.},
    isbn={9781259010286},
    series={McGraw-Hill Series in Aeronautical and Aerospace engineering},
    url={https://books.google.nl/books?id=xVsiAwAAQBAJ},
    year={2011},
    edition={5},
    publisher={McGraw-Hill}
}

@book{abbott,
    title={Theory of Wing Sections},
    subtitle={Including a Symmary of Airfoil Data},
    author={Abbott, I. H. and von Doenhoff, A. E.},
    isbn={9780486605869},
    series={Dover Books on Aeronautical Engineering Series},
    year={1959},
    publisher={Dover Pubblications}
}

chapter1.tex

%!TeX root = chapter1

\providecommand{\main}{..}

\makeatletter
\def\input@path{{\main/}}
\makeatother

\documentclass[\main/main.tex]{subfiles}

\begin{document}

    %---- Natbib + bibunit approach -----
%   \begin{bibunit}
%       \chapter{First chapter}
%       This is chapter 1 reference test: \cite{Kulfan2008}, \cite{anderson}.
%       \subsection*{References}
%       \putbib[\main/chapter1/bibgraf1]
%   \end{bibunit}
    %------------------------------------

    %---- Natbib + chapterbib approach -----
%   \chapter{First chapter}
%   This is chapter 1 reference test: \cite{Kulfan2008}, \cite{anderson}.
%   \nocite{apsrev41Control}
%   \bibliographystyle{apsrev4-1}
%   \bibliography{\main/bibgraf}
    %---------------------------------------

    %--------- Biblatex approach ----------
    \newrefsection
    \chapter{First chapter}
    This is chapter 1 reference test: \autocite{Kulfan2008}, \autocite{anderson}.
    \printbibliography
    %--------------------------------------

\end{document}

bibgraf1.bib

@CONTROL{REVTEX41Control}
@CONTROL{apsrev41Control,author="00",editor="1",pages="1",title="0",year="0"}

@article{Kulfan2008,
    author = {Kulfan, B. M.},
    title = {Universal Parametric Geometry Representation Method},
    journal = {Journal of Aircraft},
    volume = {45},
    number = {1},
    pages = {142-158},
    year = {2008},
    doi = {10.2514/1.29958},
    URL = { 
    https://doi.org/10.2514/1.29958
    },
    eprint = { 
    https://doi.org/10.2514/1.29958
    }
}

@book{anderson,
    title={Fundamental of Aerodynamics},
    author={Anderson, Jr., J. D.},
    isbn={9781259010286},
    series={McGraw-Hill Series in Aeronautical and Aerospace engineering},
    url={https://books.google.nl/books?id=xVsiAwAAQBAJ},
    year={2011},
    edition={5},
    publisher={McGraw-Hill}
}

chapter2.tex

%!TeX root = chapter2

\providecommand{\main}{..}

\makeatletter
\def\input@path{{\main/}}
\makeatother

\documentclass[\main/main.tex]{subfiles}

\begin{document}

    %---- Natbib + bibunit approach -----
%   \begin{bibunit}
%       \chapter{Second chapter}
%       This is chapter 2 reference test: \cite{Kulfan2008}, \cite{abbott}.
%       \subsection*{References}
%       \putbib[\main/chapter2/bibgraf2]
%   \end{bibunit}
    %------------------------------------

    %---- Natbib + chapterbib approach -----
%   \chapter{Second chapter}
%   This is chapter 2 reference test: \cite{Kulfan2008}, \cite{abbott}.
%   \nocite{apsrev41Control}
%   \bibliographystyle{apsrev4-1}
%   \bibliography{\main/bibgraf}
    %---------------------------------------

    %--------- Biblatex approach ----------
    \newrefsection
    \chapter{Second chapter}
    This is chapter 2 reference test: \autocite{Kulfan2008}, \autocite{abbott}.
    \printbibliography
    %--------------------------------------

\end{document}

bibgraf2.bib

@CONTROL{REVTEX41Control}
@CONTROL{apsrev41Control,author="00",editor="1",pages="1",title="0",year="0"}

@article{Kulfan2008,
    author = {Kulfan, B. M.},
    title = {Universal Parametric Geometry Representation Method},
    journal = {Journal of Aircraft},
    volume = {45},
    number = {1},
    pages = {142-158},
    year = {2008},
    doi = {10.2514/1.29958},
    URL = { 
    https://doi.org/10.2514/1.29958
    },
    eprint = { 
    https://doi.org/10.2514/1.29958
    }
}

@book{abbott,
    title={Theory of Wing Sections},
    subtitle={Including a Symmary of Airfoil Data},
    author={Abbott, I. H. and von Doenhoff, A. E.},
    isbn={9780486605869},
    series={Dover Books on Aeronautical Engineering Series},
    year={1959},
    publisher={Dover Pubblications}
}

1 Answer 1

7

Sub-bibliographies work with subfiles from version 1.5 onwards, available from Github or from CTAN. See its documentation. Note that it is no longer necessary to manipulate the path of files oneself. Below I show how to typeset sub-bibliographies with package

(A) chapterbib

(B) bibunits

(C) biblatex

For each of these scenarios, we consider two file structures.

(1) bib files in the top directory

main.tex
bib1.bib
bib2.bib
chapter1/chapter1.tex
chapter2/chapter2.tex

(2) bib files in the subdirectories

main.tex
chapter1/bib1.bib
chapter1/chapter1.tex
chapter2/bib2.bib
chapter2/chapter2.tex

The bib files contain the following entries.

% bib1.bib
@book{A,
    title={The meaning of A},
    author={A. Alpha},
    year=2019,
    publisher={Apublisher}
}

% bib2.bib
@book{B,
    title={The meaning of B},
    author={B. Beta},
    year=2019,
    publisher={Bpublisher}
}

(A1) Package chapterbib, bib files in main directory

% main.tex
\documentclass{report}
\usepackage{chapterbib}
\usepackage{subfiles}
\begin{document}
\subfileinclude{chapter1/chapter1}
\subfileinclude{chapter2/chapter2}
\end{document}    

% chapter1/chapter1.tex
\documentclass[../main]{subfiles}
\begin{document}
\chapter{First chapter}
My references: \cite{A}
\bibliographystyle{alpha}
\bibliography{../bib1,../bib2}
\end{document}

% chapter2/chapter2.tex
\documentclass[../main]{subfiles}
\begin{document}
\chapter{Second chapter}
My references: \cite{B}
\bibliographystyle{alpha}
\bibliography{../bib1,../bib2}
\end{document}

To typeset main.tex, run

pdflatex main
bibtex chapter1/chapter1
bibtex chapter2/chapter2
pdflatex main
pdflatex main

To typeset one of the chapters separately, run

cd chapter1
pdflatex chapter1
bibtex chapter1
pdflatex chapter1
pdflatex chapter1

(A2) Package chapterbib, bib files in sub-directory

Like (A1), but change the \bibliography commands to \bibliography{bib1} in chapter1.tex and to \bibliography{bib2} in chapter2.tex.

(B1) Package bibunits, bib files in main directory

% main.tex
\documentclass{report}
\usepackage{bibunits}
\defaultbibliographystyle{apalike}
\usepackage{subfiles}
\begin{document}
\subfile{chapter1/chapter1}
\subfile{chapter2/chapter2}
\end{document}

% chapter1/chapter1.tex
\documentclass[../main]{subfiles}
\begin{document}
\begin{bibunit}
\chapter{First chapter}
My references: \cite{A}
\putbib[\subfix{../bib1},\subfix{../bib2}]
\end{bibunit}
\end{document}

% chapter2/chapter2.tex
\documentclass[../main]{subfiles}
\begin{document}
\begin{bibunit}
\chapter{Second chapter}
My references: \cite{B}
\putbib[\subfix{../bib1},\subfix{../bib2}]
\end{bibunit}
\end{document}

To typeset the main document, run

pdflatex main
bibtex bu1
bibtex bu2
pdflatex main
pdflatex main

To typeset the chapters separately, run

cd chapter1
pdflatex chapter1
bibtex bu1
pdflatex chapter1
pdflatex chapter1

(B2) Package bibunits, bib files in sub-directory

Like (B1), but change the \putbib commands to \putbib[\subfix{bib1}] in chapter1.tex and to \putbib[\subfix{bib2}] in chapter2.tex.

(C1) Package biblatex, bib files in main directory

% main.tex
\documentclass{report}
\usepackage{biblatex}
\usepackage{subfiles}
\bibliography{bib1,bib2}
\begin{document}
\subfileinclude{chapter1/chapter1}
\subfileinclude{chapter2/chapter2}
\end{document}

% chapter1/chapter1.tex
\documentclass[../main]{subfiles}
\begin{document}
\newrefsection
\chapter{First chapter}
My references: \cite{A}
\printbibliography
\end{document}

% chapter2/chapter2.tex
\documentclass[../main]{subfiles}
\begin{document}
\newrefsection
\chapter{Second chapter}
My references: \cite{B}
\printbibliography
\end{document}

To typeset the main document, run

pdflatex main
biber main
pdflatex main
pdflatex main

To typeset the chapters separately, run

cd chapter1
pdflatex chapter1
biber chapter1
pdflatex chapter1
pdflatex chapter1

(C2) Package biblatex, bib files in sub-directory

Like (C1), but replace the \bibliography command in main.tex by \bibliography{chapter1/bib1,chapter2/bib2}.

Note on filenames and their paths

In general, filenames (with an optional path) have to be surrounded by \subfix{...}, as demonstrated in (B1) and (B2). The \bibliography command has this \subfix magic already built-in, therefore \subfix is not needed in (A1), (A2), (C1) and (C2) (in fact, it rather would mess up things).

8
  • 1
    Is there any way to have the bibliography files live in the chapter subdirectories? And ideally, for each of them to be able to only reference their "own" bib file? I.e., \bibliography{bib1} in chapter1/chapter1.tex Jan 28, 2021 at 16:42
  • 2
    @AndrewJaffe I have redone my answer from 2019. It now discusses your situation as well.
    – gernot
    Jan 28, 2021 at 22:09
  • 1
    thanks! I actually figured it out, but it's good to see it corroborated. Note that you accidentally call your bib files *.tex, and also that I think you need to run pdflatex on each of the chapter files (from their own directories) to create the aux files before you can run BibTeX. Jan 28, 2021 at 22:46
  • 2
    @AndrewJaffe Thanks for the note regarding file extensions, I've corrected them. Because of the \include statements (instead of \input), LaTeX generates separate aux files for the included files. I've actually tested all versions (with the correct extensions).
    – gernot
    Jan 28, 2021 at 22:53
  • 1
    Yep, exactly right about not needing the pdflatex runs -- that was a holdover from my attempts to use plain \subfile instead of \subfileinclude, which I could never get to work... Jan 28, 2021 at 23:29

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.