-1

I am a newbie, and I have been looking for the solution for a while. But if I use subfile for separate files, how can I do citations in the subfiles that refer to the global bibliography? Like if I insert a bibliography in the main tex file, that will include every citation I have in every subfiles?

Thank you very much!

bilibili
  • 1
  • 2
  • Welcome! Before I commented, someone downvoted your question, probably because it lacked a minimal working example (MWE). But I see that you have responded to a posted answer, I upvoted. However, in the future, be sure to provide code in response to code, not just ask more questions. –  Dec 30 '17 at 20:12

1 Answers1

1

Citations in subfiles that refer to a global bibliography that is set up in the main file work exactly as if there was no subfile.

The following is the file main.tex which does not contain any \cite commands, but prints the bibliography.

\documentclass{report}
\usepackage{filecontents}
\usepackage{subfiles}
\begin{filecontents}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
}
\end{filecontents}

\begin{document}
\subfile{chapter1.tex}
\bibliographystyle{unsrt}
\bibliography{\jobname}
\end{document} 

The following is the file chapter1.tex which contains the only \cite command.

\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{title}
\cite{key}
\end{document}

To get the desired result, you can compile main.tex using PDFLaTeX, BibTeX, PDFLaTeX, PDFLaTeX.


Alternative approach using \include instead of the subfiles package:

The following is a MWE that uses \include{filename} commands instead of the subfiles package:

main.tex:

\documentclass{report}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
  author = {Author, A.},
  year = {2001},
  title = {Title},
  publisher = {Publisher},
}
\end{filecontents}
\begin{document}
some test
\ref{chapter}

\include{chapter1}
\bibliographystyle{unsrt}
\bibliography{\jobname}
\end{document}

chapter1.tex:

\chapter{title}
\label{chapter}
some text
\cite{key}

Using this approach, TeXMaker auto completes \cite and \ref commands in both files equally.

Please note the differences in the chapter files in comparison to the subfiles approach. And please also keep in mind that \include{filename} does a \clearpage before and after \include{filename}.

Further information: When should I use \input vs. \include?

leandriis
  • 62,593
  • Thanks. When I use Texmaker, and type \cite command, the list of sources do not populate automatically. If I write in the main file then there is no issues. Is there any way to fix it? – bilibili Dec 30 '17 at 00:31
  • @bilibili: On my system the auto completion of citation keys generally works fine on main and sub files. There may though be problems with the MWE in my answer as it uses filecontents. If you create a MWE with a separate .bib file (and maybe press Edit --> Refresh Bibliography in TexMaker) the auto completion should work on both files. – leandriis Dec 30 '17 at 11:32
  • thank you very much. How about the ref command for labels? They normally do not populate. So what i did is to do this in the main tex file: \include{main.tex} \include{abstract.tex} \include{intro.tex} \begin{document}

    This way I can use \ref that include all the labels in the structure. Is this a legit way?

    – bilibili Dec 30 '17 at 16:09
  • @bilibili \include before \begin{document} appears strange to me. I have edited my answer to include a MWE that demonstrates the usage of \include. I would prefer this solution, as with the subfiles example TeXMaker does not auto complete \ref commands to labels in sub files. – leandriis Dec 30 '17 at 17:13
  • This approach does not allow compiling chapter1.tex with citations included, right? How to do that? The whole point of subfiles is to allow subcompilation, I believe. – Alan Mar 29 '20 at 17:24