# Get a bibliography for each subfile when compiling subfile and main file

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,
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,
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,
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,
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
\@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

\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}


(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.

• Thanks a lot for the great example! I've integrated the solution in the real project and it works! I've made a slight modification. Instead of adding the magical package and default style in the main file, I do it in the class file. The idea of uploading the MWE to Overleaf is also good, I didn't think about it. – José Manuel Martínez Martínez Feb 14 '17 at 10:34

I've tried a workaround. It is not really a solution but for my use case works.

Since the project is living in overleaf, by default the main file will be compiled. To avoid any problems, instead of using \subfile{text1.tex} I use \include{text1.tex}.

main.tex

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


Then I comment in both text1.tex and text2.tex the first two lines and the last one.

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}


It works with chapterbib so the result is as expected -- each biblio is compiled at the end of its subfile.

If one wants to compile only a subfile (working locally). Just uncommenting the same three lines allows to compile only the subfile with its bibliography.

Ideally, I would like to avoid commenting and uncommenting lines. So any alternative approach is welcome.

• Is Biblatex an option? Bibliographies per section or per segment are straightforward in that case. It all just works, rather than being an afterthought you're trying to add onto a tool not designed for it. – cfr Feb 14 '17 at 2:05
• @cfr thank you for the comment. I'm not familiar with Biblatex. It might work. But I'd rather change not too much. I am afraid it could generate some conflict with other packages. The class I am using is much more complex than the MWE and lot of packages are required. – José Manuel Martínez Martínez Feb 14 '17 at 10:25