# How to configure latex to put bibliography at end of each chapter when using subfiles?

I'd like to have the bibliography of each chapter at its end. Here is an MWE of how I built my thesis:

I have a directory tree which looks like this:

|-main.tex
|-ref.bib
|--Ch1
|---ch1.tex
|--Ch2
|---ch2.tex


And I'm using \subfiles to build the document.

main.tex

\documentclass{article}
\usepackage{subfiles}
\usepackage{natbib}
\begin{document}
\subfile{Ch1/ch1.tex}
\subfile{Ch2/ch2.tex}
% Experimenting with configurations of bibliography
%   \bibliography{ref}
\end{document}


Ch1/ch1.tex

\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Chapter 1}
I am a reference from chapter 1.\citep{lamport1994latex}
\bibliographystyle{agsm}
\bibliography{ref}
\end{document}


Ch2/ch2.tex

\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Chapter 2}
I am a reference from chapter 2.\citep{goossens1997latex}
\bibliographystyle{agsm}
\bibliography{ref}
\end{document}


ref.bib

@article{lamport1994latex
title={LATEX user’s guide and reference manual},
author={Lamport, Leslie},
year={1994},
}
@book{goossens1997latex,
title={The LATEX graphics companion: illustrating documents with TEX and PostScript},
author={Goossens, Michel and Rahtz, Sebastian PQ and Mittelbach, Frank},
volume={3},
year={1997},
}


I've tried several possibilities of \bibliography{ref.bib} in the three files and none of them produced a desirable output, which would be as I expect: two bibliographies at all; one at the end of chapter 1 and one at the end of chapter 2, with only lamport1994latex in the first bibliography and only goossens1997latex in the second bibliograhy. Is this possible?

I suggest you to use another type of bibliography, e. g. putting it directly at the end of your chapter. Dividing your ref.bib shouldn't be that much work for not writing your thesis like in a pandemonium.

Version 1 (bibliography at the end of the part with a blank page in between):

main-v1.tex:

\documentclass{book}
\usepackage{lipsum}
\begin{document}
\include{one}
\include{two}
\end{document}


one-v1.tex:

    \chapter{One}
\lipsum[1]\cite{lamport1994latex}
\begin{thebibliography}{One}
\bibitem [Lamport 1994]{lamport1994latex}Leslie Lamport: LATEX user’s guide and reference manual, Reading, Massachusetts 1994, Addison-Wesley.
\end{thebibliography}


two-v1.tex:

    \chapter{Two}
\lipsum[2]\cite{goossens1997latex}
\begin{thebibliography}{Two}
\bibitem [Goossens \textit{et al.} 1997]{goossens1997latex}Michel Goossens, Sebastian P. Q. Rahtz, Frank Mittelbach: The LATEX graphics companion: illustrating documents with TEX and PostScript, Vol. 3, 1997, Addison-Wesley Professional.
\end{thebibliography}


Version 2 (bibliography directly at the end of the part):

main-v2.tex:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\include{one}
\include{two}
\end{document}


one-v2.tex:

    \part{One}
\lipsum[1]\cite{lamport1994latex}
\begin{thebibliography}{One}
\bibitem [Lamport 1994]{lamport1994latex}Leslie Lamport: LATEX user’s guide and reference manual, Reading, Massachusetts 1994, Addison-Wesley.
\end{thebibliography}


two-v1.tex:

    \part{Two}
\lipsum[2]\cite{goossens1997latex}
\begin{thebibliography}{Two}
\bibitem [Goossens \textit{et al.} 1997]{goossens1997latex}Michel Goossens, Sebastian P. Q. Rahtz, Frank Mittelbach: The LATEX graphics companion: illustrating documents with TEX and PostScript, Vol. 3, 1997, Addison-Wesley Professional.
\end{thebibliography}


Note: You can't use \chapter in an article without defining it. You can use \part, \section, \subsection, \subsubsection, \paragraph, \subparagraph.

I also recommend using the simple \include command instead of an additional package.

If you also sort your folders and files by type and origin/destination (e. g. graphics for theory, graphics from measured data, tex files in mainfolder, ...) with all titles at lowercase, you'll definitely will have a way better overview about your thesis and your work progress.