# Handle .bib in subfiles

I have a problem very similar to the question being asked in Bibliographies when using subfiles . My .bib is references in main.tex. When compiling main, everything works like a charm. Working on the subfiles, I have two issues though (which are most likely related):

1. I can't access the bib with auto-completion
2. When compiling the subfile, it doesn't insert the citations.

I tried including

\bibliographystyle{plainnat}
\bibliography{mybib}

in the subfiles, which also worked smoothly. Obviously, the bibs are included in every chapter of the document when compiling main.tex, then. I tried setting a global boolean in main.tex:

\newboolean{printBibInSubfiles}
\setboolean{printBibInSubfiles}{false}

and in the subfiles:

\ifthenelse{\boolean{printBibInSubfiles}}
{}
{\bibliographystyle{plainnat}
\bibliography{../betterbib.bib}}

For some reason, then setting the boolean to false, the "bibliography"-header was printed in the subfiles as well as for the main file, but there was no output of citations under any of the "bibliography"-headers. Furthermore, compiling only the subfiles didn't work correctly.

My document structure is the following:

main.tex:

\begin[document]
...
\subfile{sub/dataprocessing}
...
\bibliographystyle{plainnat}
\bibliography{mybib}
\end{document}

the subfiles:

\documentclass[../main.tex]{subfiles}
\begin{document}
...
\cite{Random2014}
...
\end{document}

After all this explanation: the only thing I want is to access the .bib from the subfiles without having to manually uncommenting something when compiling the main file. Any takers?

PS: Obviously, I also tried the solution provided in "Bibliographies when using subfiles" but couldn't get it to work. I don't know whether that would solve my problem anyway, since I don't have the \printbibliography-issue of the thread opener.

• I seems to me that the "Bibliographies when using subfiles" should solve your problem, as you could just redefine \printbibliography in the main file, thereby removing the bibliography from the subfiles. What was your problem with that solution? – Dolphin Dec 11 '14 at 19:22
• Hey Dolphin, thanks for your comment. I can't recall, why it didn't compile - most likely I made a mistake myself (that was a few weeks ago). Unfortunately, by now I've used natbib throughout my thesis (also using \citalt etc.). Therefore, switching back to the biblatex package is not a real option... :( Do you think, the proposed boolean-solution could work out? – Klaster Dec 12 '14 at 12:39
• Hi. Since I haven't tried it I can't say for certain, but yes I think it would work. – Dolphin Dec 15 '14 at 12:18
• The "autocompletion" and such comments strongly suggest this is a editor (or better IDE) problem. E.g. with xemacs and Auc-TeX this works without a hitch if the right trailers (giving the master file) are included in each piece. – vonbrand Jan 27 '16 at 2:39

## 3 Answers

For posterity, I was having the same issue - and the answer from 'bibliographies when using subfiles' didn't work for me because I wasn't using biblatex and didn't have the \printbibliography command.

I found a nice, working solution at latex-community.org, slightly modified*:

main.tex:

\documentclass[...]{...}
\usepackage{natbib}
\usepackage{subfiles}
...
\providecommand{\main}{.}  % *Modification: define file location
\def\biblio{\bibliographystyle{plainnat}\bibliography{\main/bibliography_file}}  % *Modification: added \main/ to specify relative file location.
\begin{document}
\def\biblio{}
...
\subfile{chapter_1}
\subfile{chapter_2}
...
\bibliographystyle{plainnat}
\bibliography{bibliography}
\end{document}

tex/sub.tex:

\providecommand{\main}{..}  % *Modification: redefine path location, must go before \documentclass
\documentclass[main]{subfiles}
\begin{document}
...
\biblio
\end{document}

*Modifications are to let a bibliography file in a different directory than the subfile (sub.tex) work. If your sub.tex file is in the same directory as ms.tex and bibliography_file then you don't need this.

• It is preferable to post working code. Can you turn your fragments into a complete example? – cfr Dec 28 '15 at 2:10

Solved this by using biblatex like this in the main file with the .bib files added in the preamble.

\usepackage[backend=biber]{biblatex}
\addbibresource{bibliography_file.bib}

\begin{document}

\subfile{subfile_with_citations}
\printbibliography

\end{document}
• Welcome to TeX.SX! This is more of a comment than an answer to the question; however it shows an alternative way that might be good to future readers. – egreg Jun 29 '16 at 13:21

Another one for posterity sake. This is for situations where one would like to utilize the same logic as the OP. According to the subfiles package documentation, the subfiles will take everything except for the content enveloped in the \begin{document} and \end{document} section. So setting the boolean to false in there and true otherwise would make it work. In ShareLatex, you can do so by leaving the .bib file in the root folder of your project and referencing it in the main.tex like this:

main.tex:

\usepackage[natbibapa]{apacite} % Any preferred package would work
\newboolean{printBibInSubfiles}
\setboolean{printBibInSubfiles}{true}
\def\bib{\ifthenelse{\boolean{printBibInSubfiles}}
{\bibliographystyle{apacite}\bibliography{ThesisRef}}
{}
}
...
\begin{document}
\setboolean{printBibInSubfiles}{false}
...

subfile_1.tex:

\documentclass[../my_dir/main.tex]{subfiles}
\begin{document}
...
\bib
\end{document}
• Needs \usepackage{ifthen} too – Eric May 15 '17 at 13:11