[Tex/LaTex] Get a bibliography for each subfile when compiling subfile and main file

natbiboverleafsubfiles

I have a project with multiple files (in overleaf). I am using subfiles because I want collaborators to be able to compile their subfile when working offline. But at the same time to be able to compile the whole project. We used a similar structure without subfiles but using include and chapterbib. That compiled the whole project without problems, but it is not possible to compile each subfile individually.

My aim is to have a bibliography for each subfile (not a bibliography for the whole project).

The situation right now is that I can compile each subfile and its bibliography is generated for the citations used. However, when I try to compile the main project file something goes wrong, it seems that it tries to use the bibliography of the first subfile for the second.

The structure is as follows:

├── bibs # folder to save a bib file per subfile
│   ├── text1.bib
│   └── text2.bib
├── main.tex # main latex project file
├── misc
│   └── bibnosec.sty # a little patch
├── my-class.cls # my class
├── text1.tex # a subfile
└── text2.tex # another subfile

MWE:

bibs/text1.bib

@inproceedings{Kermes2016,
    address = {Portoroz, Slovenia},
    author = {Kermes, Hannah and Knappen, J{\"{o}}rg and Degaetano-Ortlieb, Stefania and Teich, Elke},
    booktitle = {In Proceedings of the Ninth International Conference on Language Resources and Evaluation (LREC'16)},
    title = {{The Royal Society Corpus: From Uncharted Data to Corpus}},
    year = {2016}
}

@inproceedings{Khamis-etal2015,
    address = {Lancaster},
    author = {Khamis, Ashraf and Degaetano-Ortlieb, Stefania and Kermes, Hannah and Knappen, J{\"{o}}rg and Ordan, Noam and Teich, Elke},
    booktitle = {Corpus Linguistics 2015},
    month = jul,
    title = {{A resource for the diachronic study of scientific English: Introducing the Royal Society Corpus}},
    year = {2015}
}

bibs/text2.bib

@inproceedings{Khamis-etal2015,
    address = {Lancaster},
    author = {Khamis, Ashraf and Degaetano-Ortlieb, Stefania and Kermes, Hannah and Knappen, J{\"{o}}rg and Ordan, Noam and Teich, Elke},
    booktitle = {Corpus Linguistics 2015},
    month = jul,
    title = {{A resource for the diachronic study of scientific English: Introducing the Royal Society Corpus}},
    year = {2015}
}

@inproceedings{Degaetano-etal2015b,
    address = {RWTH Aachen University},
    author = {Degaetano-Ortlieb, Stefania and Kermes, Hannah and Khamis, Ashraf and Knappen, J{\"{o}}rg and Ordan, Noam and Teich, Elke},
    booktitle = {"Challenging Boundaries" - 42nd International Systemic Functional Congress (ISFCW2015)},
    month = jul,
    title = {{Information Density in Scientific Writing: A Diachronic Perspective}},
    year = {2015}
}

misc/bibnosec.sty

\ProvidesPackage{bibnosec}[2013/07/08 Bibliographies without sections (JKn)]
% This is the thebibliography environment from article.cls with
% two lines commented out
\renewenvironment{thebibliography}[1]
     {%\section*{\refname}%
      %\@mkboth{\MakeUppercase\refname}{\MakeUppercase\refname}%
      \list{\@biblabel{\@arabic\c@enumiv}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \@openbib@code
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy
      \clubpenalty4000
      \@clubpenalty \clubpenalty
      \widowpenalty4000%
      \sfcode`\.\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist}
\endinput

main.tex

\documentclass{my-class}
\begin{document}
\subfile{text1}
\subfile{text2}
\end{document}

my-class.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{my-class}[2017/02/13 My MWE class]

% PAGE FORMAT
\LoadClass[a4paper,10pt,twoside]{article}

\RequirePackage{subfiles}

% BIBLIOGRAPHY
% bibliography management
\RequirePackage[sectionbib]{natbib}
% to enable multiple bibliographies in a document
\RequirePackage[sectionbib]{chapterbib}
% custom local package
\RequirePackage{misc/bibnosec}

text1.tex

\documentclass[main.tex]{subfiles}
\begin{document}
\section{This is section 1}
See \cite{Kermes2016} or \cite{Khamis-etal2015} for examples.
\subsection*{References}
\bibliographystyle{apalike}
\bibliography{bibs/text1}
\end{document}

text2.tex

\documentclass[main.tex]{subfiles}
\begin{document}
\section{This is section 2}
See \cite{Degaetano-etal2015b} or \cite{Khamis-etal2015} for examples.
\subsection*{References}
\bibliographystyle{apalike}
\bibliography{bibs/text2}
\end{document}

Best Answer

(You can share a read-only URL of your Overleaf project; other users can then clone the files and edit them right-away in the copy ;-)

The bibunits package seems to work better than chapterbib in this case. Here's what I did:

main.tex

\documentclass{my-class}
\usepackage{bibunits} % <-- the magical package!
%% Assuming all subfiles will use the same style
\defaultbibliographystyle{apalike}
\begin{document}
\subfile{text1}
\subfile{text2}
\end{document}

text1.tex

\documentclass[main.tex]{subfiles}
\begin{document}
\begin{bibunit}  % <-- add this line
\section{This is section 1}
See \cite{Kermes2016} or \cite{Khamis-etal2015} for examples.
\subsection*{References}
\putbib[bibs/text1]  % <-- your .bib file here!
\end{bibunit}    % <-- add this line
\end{document}

and similarly for text2.tex.

A working example can be found at https://www.overleaf.com/read/fscqhqtyhhpk. Click on the "PROJECT" in the top bar to toggle the file list panel; and "Clone this project" for editable copy.

Related Question