# Bibliographies when using subfiles

I am writing my Ph.D. thesis in XeLaTeX with Biblatex, and there are times when I need to compile individual chapters, and times when I need to compile the entire document, so I use subfiles, which works wonderfully. However, in each of these chapters I have a \printbibliography command, which I would like to turn off when compiling the final document, so that only the \printbibliography command is run in the mainfile.

The file structure is something like:

Main.tex:

\usepackage{subfiles, biblatex}
\begin{document}

\subfile{Chapter1.tex}

\printbibliography
\end{document}


Chapter1.tex:

\documentclass[Main.tex]{subfiles}
\begin{document}
\cite{Someguy1981}
\printbibliography
\end{document}


## 5 Answers

From subfiles.sty the \subfile command is defined as:

\newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup}


So it is straightforward to hook in some code to locally redefine \printbibliography to do nothing. For an example add the following to the preamble of the main file:

\makeatletter

\newrobustcmd*{\nobibliography}{%
\@ifnextchar[%]
{\blx@nobibliography}
{\blx@nobibliography[]}}

\def\blx@nobibliography[#1]{}

\appto{\skip@preamble}{\let\printbibliography\nobibliography}

\makeatother


Note that biblatex makes bibliographic data available via citation commands. So unless you want to view the bibliography entries outside the main file, the subfiles need not invoke \printbibliography.

• I have tested this and it works perfectly. Apr 5 '13 at 23:39
• This stuff goes in the main file or in the subfile? Does it works with standard LaTeX or has something XeLaTeX specific? Jun 22 '14 at 2:29
• It appears that this solution no longer works due to internal refactoring of the subfiles package.
– yun
Jul 3 '20 at 22:58

I also used Audrey's approach (https://tex.stackexchange.com/a/107111/85983) for a while, but I got annoyed of having to write \printbibliography at the end of each subfile. Now I'm using the following Latex Hooks in the preamble of my main file:

%%% Default start and end for each subfile
\AtBeginDocument{%
}

\AtEndDocument{%
\printbibliography
\listoffigures
\listoftables
\newpage
\printacronyms[include-classes=abbrev,name=Acronyms]
}


Borrowing a post on this Latex Community thread, you can define your \printbibliography command in the preamble and then un-define it in the main document. Your main file will then contain its own call to \bibliography{} and \biblographystyle.

Something like:

Main.tex

\usepackage{subfiles, biblatex}
\newcommand{\dobib}{ % Define the command
\bibliographystyle{acm}
\bibliography{../../references} % Place the path relative to the subfile here
}

\begin{document}
\renewcommand{\dobib}{} % Un-define the command

\subfile{Chapter1.tex}

\bibliographystyle{acm}
\bibliography{../references} % Place the path relative to the main file here
\end{document}


Chapter1.tex

\documentclass[Main.tex]{subfiles}
\begin{document}
According to SomeGuy \cite{Someguy1981} pigs can fly.

\dobib
\end{document}


This works fine for me.

Another approach could be to distinguish between the subfiles and the main file by their jobname. Say your main file is called Thesis.tex, then the jobname is normally Thesis and you can test

\IfEq{\jobname}{\detokenize{Thesis}}{}{%
\AtEndDocument{%
\printbibliography%
}
}


this will insert a bibliography at the end of the subfiles and do nothing in your main file.

(if possible use this before \usepackage{biblatex}, otherwise it might cause a warning, see undefined references warning for \AtEndDocument{\printbibliography})

Since version 2.0 of subfiles, one can also use the condition macro \ifSubfilesClassLoaded{}{} to do something similar as Audrey's approach (https://tex.stackexchange.com/a/107111/85983).

With my setup, a MWE is as follows:

main.tex

\documentclass{book}
\usepackage{biblatex}
\usepackage{subfiles} % must be last usepackage

\providecommand{\topdir}{.}
\addglobalbib{\topdir/references.bib} % topdir is needed here so that
% we can resolve the path in the subfile
% correctly. There we re-define the
% topdir macro to the location of the
% bib file.

\begin{document}
\subfile{dir/sub}
\printbibliography[heading=bibintoc]{}
\end{document}


sub.tex

\providecommand{\topdir}{..} % reset all paths to location of main.tex
\documentclass[../main.tex]{subfiles}

\begin{document}
% some content with citations

\ifSubfilesClassLoaded{%
\printbibliography{}
}{} % we have no 'else' action
\end{document}