[Tex/LaTex] Handle .bib in subfiles

bibtexsubfiles

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.

Best Answer

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.

Related Question