14

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.

4
  • 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, 2014 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, 2014 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, 2014 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, 2016 at 2:39

5 Answers 5

7

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.

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

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
\usepackage{ifthen}
\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}
2
  • 1
    Needs \usepackage{ifthen} too
    – Eric
    May 15, 2017 at 13:11
  • 1
    This works well! Note that you don't want to use \bib in the main file though. There, it should still be \bibliography as usual. Also to note is that of course ThesisRef is a placeholder for the actual bib files
    – lucidbrot
    Jul 15, 2021 at 15:17
3

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}
1
  • 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, 2016 at 13:21
1

I was having trouble getting autocompletion to work, what finally fixed it was using subfix from the subfiles package.

In the main .tex file:

\documentclass{article}

% --------------------------------------------------------\
% imports

\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage[
    backend=biber,
    style=alphabetic,
    citestyle=authoryear
]{biblatex}

\usepackage{subfiles}

% --------------------------------------------------------
% Bibliography
\addbibresource{\subfix{../prelims_lit.bib}}

% Print bibliography at end of each subfile when compiling separately,
% but also in main document when compiling together
\AtEndDocument{%
    \printbibliography
}

\begin{document}
\subfile{sections/0_abstract}
...
\end{document}

and nothing fancy needed in any of the subfiles. I think this should work even if the .bib file isn't in the parent directory (as in this case) because it seems like for some autocompletion plugins it just has trouble resolving the relative path when the subfile is in another directory.

0

None of the previous answers seemed to help in my case with the main file in root folder, subfiles together in a separate child folder. I kept getting citation not found errors (even when using \subfix) so I fixed it by simply creating a symbolic link to my .bib file in my subfolder.
A bit dirty but no code modifications required :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .