# How to get a bibliography to work in subfiles?

I'm trying to get a bibliography to work while using subfiles for my dissertation. My goal is to have one main bibliography for all chapters. The current structure of my files is:

1. Main Folder

• Thesis.txt

• Thesis.bib

2. Introduction Folder

• Introduction.txt

I have tried several different methods with natbib and biblatex, including most highly rated answer from this question (Handle .bib in subfiles), with no luck. My bibliography is available in the main Thesis.txt file and if I try to \cite something there it's fine. However when I try and cite something in the subfile (Introduction.txt), \cite will auto fill but I always get the error for undefined citations. This is also the case if I try and compile the main Thesis.txt file with the subfile Introduction.txt.

My current simplified code is as such:

Thesis.txt

\documentclass{ucetd}
\usepackage[super,comma]{natbib}
\bibliographystyle{unsrtnat}
\usepackage{subfiles}

\providecommand{\main}{.}  \
\def\biblio{\bibliographystyle{unsrtnat}\bibliography{\main/Thesis}}

\begin{document}
\def\biblio{}

\renewcommand\bibname{References}
\newpage
\begin{singlespace}
\bibliography{Thesis}
\end{singlespace}

\end{document}


Introduction.txt

\providecommand{\main}{..}  % *Modification: redefine path location, must go before \documentclass
\documentclass[\main/Thesis.tex]{subfiles}

\begin{document}

some text. \cite{}

\newpage
\biblio
\end{document}


I have also tried including the bibliography in the subfile and this still does not resolve the error. Moreover I tried the biblatex exaple(Handle .bib in subfiles) and that did not work either.

With recent versions of the subfiles package, the general rule is

Choose the path relative to the file it appears in.

The following example works with a current LaTeX distribution.

%%%%%%%% thesis.tex
\documentclass{article}
\usepackage[super,comma]{natbib}
\bibliographystyle{unsrtnat}
\renewcommand\bibname{References}
\usepackage{subfiles}
\begin{document}
\subfile{introduction/introduction}
\bibliography{thesis}
\end{document}

%%%%%%%% thesis.bib
@Article{TheSource,
author =   {T.Sourcerer},
title =    {The Source Of Everything},
journal =      {Resources},
year =     {2021}
}

%%%%%%%% introduction/introduction.tex
\documentclass[../thesis]{subfiles}
\begin{document}
Some text~\cite{TheSource}.

\bibliography{../thesis}%
}{}
\end{document}


Now you can either typeset the main document with

pdflatex thesis
bibtex thesis
pdflatex thesis
pdflatex thesis


or the introduction in the subdirectory with

cd introduction
pdflatex introduction
bibtex introduction
pdflatex introduction
pdflatex introduction


There are also several examples for generating sub-bibliographies in the subfiles-repository on Github, see the subdirectories biblatex, bibunits and chapterbib in the subdirectory test.

• I appreciate the answer. I'm a bit confused by \ifSubfilesClassLoaded. I'm not sure how to code something like that. Additionally, with TexMaker I have the issue that the .bib file needs to have the name of the .tex file. I'm not sure why and I think this is preventing my bibliography from being loaded into the subfile. If I take the exact code that I currently have and put it in Overleaf it works just fine. The only work around I've found that actually has worked so far is to make separate correspondingly named .bib files in each subfile directory. Apr 13 at 20:39
• Ok so I don't know why this works or does not work, but if I use the command line input you mention for bibtex on my subfiles, I can get the bibliography in the subfile just fine. However when I try and do this through TexMaker directly, I can't get the bibliography in the subfile. So I have to run the command line bibtex for the subfile after adding in new citations each time if I want to have it visible in the PDFLatex run through TexMaker. Apr 13 at 22:02
• @ShanKasal This seems to be a configuration problem of TeXmaker. Maybe this helps: https://tex.stackexchange.com/a/119823/110998 Apr 14 at 9:01
• \ifSubfilesClassLoaded has been introduced in November 2020 by the subfles package. If it doesn't work, then you use an older version; check in the log file. Apr 14 at 9:41