1

I am assembling my final dissertation from some articles I have written over the past years. I am using subfile and a main.tex file to collect all the preambles in one place and just plug in all the articles. Every individual article/chapter sits in its own folder with a bunch of other subfiles and images, while the massive bib file that I use for all projects sits in the upper level folder with respect to main.tex. A schematic folder tree can be the following:

  • all_tex_projects folder
    • bibliography.bib
    • thesis folder
      • main.tex
      • chap1 folder
        • chap1.tex
        • plots folder with some files
      • chap2 folder
        • chap2.tex
        • tabs folder with external tex
        • plots folder with some graphs

I would like to be able to compile single files individually as well as the main document, with graphs, external tables, and bibliographic references (although the full list of references will be only on the compiled main document). I found ways of having the plots paths change but that breaks the references and vice versa. Is there a comprehensive, elegant solution to this?

MWE:

MAIN.tex

\documentclass{report}
\usepackage[backref=true,
            backend=biber,
            style=authoryear-comp,
            url=false,
            doi=false,
            isbn=false,
            eprint=false,
            language=auto,
            natbib=true
            ]{biblatex}
\addbibresource{../bibliography.bib}
\usepackage{csquotes}
\usepackage{subfiles}

\begin{document}

\chapter{Chapter I} %\graphicspath{{./chap1/plots}{plots/}} \subfile{./chap1/chap1.tex}

\chapter{Chapter II} %\graphicspath{{./chap2/plots}{plots/}} \subfile{./chap2/chap2.tex}

\end{document}

Chap1.tex

\documentclass[../main.tex]{subfiles}
\begin{document}
Some text with a citation from the all-encompassing bib \citep{item} to illustrate next table \ref{tab_in_chap2} with relative path

subfile{../chap2/tables/table.tex}

and a plot too, local to this file

\begin{figure} \includegraphics{./plots/a_plot.pdf} \end{figure}

Last, a piece of tex from this local folder \subfile{./chap1/tables/tab.tex}

\end{document}

Ceschi
  • 11
  • sort answer: docmute and import. See here. See also here for related packages. – Fran Oct 05 '20 at 19:13
  • kind of spot on, but then the advantage of subfiles is lost: I'd need to keep track manually of all packages and repeat the preamble every time. I wonder if there's a sweet spot that strikes simplicity and flexibility. – Ceschi Oct 05 '20 at 23:56
  • Advantage? With docmute you also only have to worry about ONE preamble, that of the main document. To share this preamble (or only part of it) with child documents to be compiled independently, is as easy as put it in a separate file and \input it in each document. The problem here could be references to external documents, You can use xr-hyper for this, but I switch external to internal references when used as child documents is another story. – Fran Oct 06 '20 at 07:00
  • @Fran still I don't clearly get how I should structure each .tex file and if docmute and import would handle the .bib file upstream the main file... – Ceschi Oct 07 '20 at 19:07
  • Have you tried the option refsegment=chapter and then \printbibliography[segment=\therefsegment, heading=subbibliography ] at the end of each chapter? Another way could be \begin{refsection}\subimport{whatever/}{whatever} \end{refsection} in the main document ad just \printbibliography at the end of the subdocuments. – Fran Oct 08 '20 at 00:50

1 Answers1

0

You can use the xr package.

Clean solution: In the main file, include the lines

\usepackage{xr}
\usepackage{subfiles}
\externaldocument[M-]{\subfix{main}}% must appear after \usepackage{subfiles}

In the subfiles, prefix all references that are supposed to show the same value as when typesetting the main document, with M-. This means: For labeling, you write \label{XXX}, and for referencing, you write \ref{M-XXX} and \pageref{M-XXX}.

Before typesetting the subfiles, you have to typeset the main file a few times to get a stable main.aux containing the correct references.

Dirty solution: If you want to avoid modifying all references from XXX to M-XXX, omit the option M- in the \externaldocument command. The downside is that you get multiply defined label warnings, and you should check whether your \ref/\pageref commands show the value you expect. The background is that the labels are indeed multiply defined, once with the number/page number from the main document and once with the number from the subfile. It seems that the number from the main document trumps the one from the subfile, so you get what you want, but it may be different in a changed setting.

gernot
  • 49,614
  • Does the M- option work for stuff like \citet (with bib file upstream) and \includegraphics? – Ceschi Oct 07 '20 at 19:09
  • @Ceschi Not sure what you mean. The xr package deals with labels set with \label and references by \ref and \pageref. The \citet command refers to bib entries, and \includegraphics to images; both are unrelated to xr, \label and \ref. If you mean whether the subfiles package can handle bibliographies and images: yes, see the documentation for examples. Or ask another question explaining what you actually are aiming at. – gernot Oct 07 '20 at 19:21
  • I was not clear, my bad: xr works for cross-referencing across tex files, but that's part of my problem. I'd like to manage references, images, and tables in a fairly structured way for the main main document, being able to compile the whole project and every single document independently -- actually the chapters are reasonably self-contained, but I manage a single file with all references for all my different TeX documents – Ceschi Oct 08 '20 at 08:21
  • @Ceschi Still not clear. By "a single file with all references", do you mean the bibliography file? Do you want to have separate bibliographies at the end of each subfile if typeset on its own, but at the end of the whole document if typesetting the main document? Take a look at the documentation of the subfiles package and at the test cases on Github. Among the tests, there are several examples of how to do chapterwise bibliographies. – gernot Oct 08 '20 at 14:48
  • I have one single bib file with all my entries that I use for all the projects I write, but it sits upstream with respect to the main.tex file. As of placing, I guess the simplest way is to have a full bibliography printed at the end of the dissertation -- maybe grouped by chapter, but that's for later – Ceschi Oct 08 '20 at 16:41
  • What's the problem of adding the relative path to the name of the bibliography file in the \bibliography command, or whatever you are using. If you are still stuck, post a new question with sample code such that we understand which package you use for generating the bibliography, like bibtex vs biblatex/biber. – gernot Oct 09 '20 at 00:11