Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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[colorlinks=true]{hyperref}    
\usepackage[backend=biber,style=numeric]{biblatex}    
    \addbibresource{bib/bibliography.bib}
\begin{document}    
    \subfile{tex/childdoc.tex}    
    \printbibliography[heading=bibintoc]    
\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, 2015 at 18:41 Hi... thanks for response. I added \printbibliography to childdoc.tex... and still get the same output. – Quantifeye Mar 30, 2015 at 18:59 found this in log file Package auxhook Warning: Cannot patch \document, (auxhook) using \AtBeginDocument instead. – Quantifeye Mar 30, 2015 at 19:29 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, 2015 at 19:56

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

Bibliographies when using subfiles subfiles inside a subfile using relative paths

As Before

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

in the ./rootdoc.tex file

\documentclass{book}
\usepackage{subfiles}
\usepackage[colorlinks=true]{hyperref}
\providecommand{\main}{.}
\usepackage[backend=biber,style=numeric]{biblatex}
    \addbibresource{\main/bib/bibliography.bib}
\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} 
    \printbibliography[heading=bibintoc]
\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[colorlinks=true]{hyperref}    
\usepackage[backend=biber,style=numeric]{biblatex}    
%Use absolute paths to .bib-files for the subfiles to understand their location:
    \addbibresource{/home/Username/Dropbox/Thesis/TeX_code/thesisReferences.bib}
\begin{document}    
    \subfile{tex/childdoc.tex}    
    \printbibliography[heading=bibintoc]    
\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 – samcarter_is_at_topanswers.xyz Mar 7, 2019 at 21:05