# Subfiles and Referencing

I am having some trouble getting a modular document to work.

This is what my files look like at the moment

./rootdoc.tex

./tex/childdoc.tex

./bib/bibliography.bib

the files look like...

./rootdoc

\documentclass{book}
%some packages
\usepackage{subfiles}
\usepackage[backend=biber,style=numeric]{biblatex}

\begin{document}
\subfile{tex/childdoc.tex}
\end{document}


./tex/childdoc

\documentclass[../rootdoc.tex]{subfiles}
\begin{document}
\chapter{Sample Chapter}
some text that i have to cite\cite{ross}
\printbibliography
\end{document}


When compiling the root document everything works fine and I get the following

But when I compile .\tex.childdoc I get

Any help will be greatly appreciated.

• Sorry, but .... What is the matter? The child document have a cite that obviously cannot be compiled as a standalone document since you are not using \printbibliography here. Subfiles load the preamble, not the end of the document. Beside this, all seem the expected ...
– Fran
Mar 30 '15 at 18:41
• Hi... thanks for response. I added \printbibliography to childdoc.tex... and still get the same output. Mar 30 '15 at 18:59
• found this in log file Package auxhook Warning: Cannot patch \document, (auxhook) using \AtBeginDocument instead. Mar 30 '15 at 19:29
• when I place the childdoc.tex in the same folder as the rootdoc.tex then it works fine. Mar 30 '15 at 19:35
• Because the child document expect, as the main document, the bibliography file in ./bib/ (i.e., inside tex/), not in ../bib/ (i.e, besides tex/), I guess...
– Fran
Mar 30 '15 at 19:56

This is not my solution, but I thought I would combine two other solutions that I found on the site.

As Before

./rootdoc.tex
./tex/childdoc.tex
./bib/bibliography.bib


in the ./rootdoc.tex file

\documentclass{book}

\usepackage{subfiles}

\providecommand{\main}{.}
\usepackage[backend=biber,style=numeric]{biblatex}
\makeatletter
\newrobustcmd*{\nobibliography}{%
\@ifnextchar[%]
{\blx@nobibliography}
{\blx@nobibliography[]}}
\def\blx@nobibliography[#1]{}
\appto{\skip@preamble}{\let\printbibliography\nobibliography}
\makeatother

\begin{document}
\subfile{./tex/childdoc.tex}
\end{document}


and in the ./tex/childdoc.tex file

\providecommand{\main}{..}
\documentclass[../rootdoc.tex]{subfiles}
\begin{document}
\chapter{Sample Chapter}
some text that i have to cite\cite{ross}
\printbibliography
\end{document}


now the correct .bibfile is referenced and when child is compiled the bibliography does not appear twice when the root document is compiled.

I have spend some hours with this problem too. I basically just want to be able to compile single subfiles in order to have the possibility of sending single chapters to my thesis supervisor during my writing process.

The different solutions from the two links provided by Quantifeye are definitely more flexible, but my solution below is very simple and works for my workflow:

Simply just use the absolute path to your .bib-file when calling \addbibresource{...} in the preamble, i.e. do something like

\documentclass{book}
%some packages
\usepackage{subfiles}
\usepackage[backend=biber,style=numeric]{biblatex}
%Use absolute paths to .bib-files for the subfiles to understand their location:

\begin{document}
\subfile{tex/childdoc.tex}
\end{document}


and in the subfiles

\documentclass[../rootdoc.tex]{subfiles}
\begin{document}
\chapter{Sample Chapter}
some text that i have to cite\cite{ross}
%\printbibliography %Comment or un-comment depending on desired output
\end{document}


If I want my supervisor to see the references I will of course add the \printbibliography to the subfile, but my main concern has just been to compile anything else without my citations looking weird or having my editor throw me an error message.

NOTE: This solution is obviously not optimal if you work on a shared document, or if you want to work on more machines with different file systems.

It is however a very simple solution and it works well for me and my requirements to my workflow.

• In case you are tired about manually commenting and un-commenting the \printbibliography at the end of the subfiles, you could automatically add to all subfiles: tex.stackexchange.com/a/358429/36296 Mar 7 '19 at 21:05