# How to best use and compile multiple .tex files as part of same final document?

I have a large document which currently is comprised of a variety of .tex files.

The mainfile.tex looks something like:

%lots of total formatting stuff

\include{Body/chapter1}
\include{Body/chapter2}
%\include{Body/chapter3}
%\include{Body/chapter4}
%etc


However, this means each of the components (ie chapter1, chapter2, etc) are not valid code.

I want to be able to view an individual file and compile while viewing it (ex. chapter1.tex) within TeXmaker. I am fine if this compiles mainfile.tex

As I understand, I need to either combine everything into one file or do either:

• tell TeXmaker to let me set a "always compile this file even if not viewing it" option somewhere - so things like F1, etc work, and always reference the mainfile.tex

or

• move all my formatting initialization, etc, to a file which I can include at the front of each individual component (and include some sort of header guard in this? I have no idea if this concept is feasible or even makes sense in LaTeX) so the same overall formatting is ALWAYS included at the beginning of a document regardless as to the order they are included.

I would strongly prefer the second option if possible but I am unsure how to do this.

-
Do you know about \includeonly{..}? With this you can select which \include-d files get really included. If you put your front (ToC, etc.) and back-matter (appendix etc.) also in own file and \include them you can compile things chapters. See e.g. Keep chapter number of chapters inserted with \include for details. – Martin Scharrer Jan 18 '13 at 20:53

Have your main document in the current editing window. Now, select Options>Define Current Document as "Master Document". This should do what I understand you are asking for. You will now see in the left pane the structure of the document, and regardless of what subfile you are editing, running a compile will compile your main document.

-
Awesome. I have a feeling that's by far the easier of my two options... – enderland Jan 18 '13 at 20:33
Argh, I've been ages clicking back to the main document to compile the whole thing, this tip is going to save me millions of clicks! THANKS! – Dan May 20 '13 at 15:08
My life just became so much more efficient. You, sir or madame, are truly wonderful. – Dylan Knowles Jan 17 '14 at 19:01
Using texmaker for 4 or 5 years, this problem always bugged me. Today I finally look for a solution and here you are. Props on you sir. – Al_th Jul 8 '14 at 8:27
Is there anyway to always keep the full tree open instead of making the current .tex file the new root node? I find the only way to get back to the root structure is to click the back arrow a bunch of times – SwimBikeRun Feb 4 at 6:37

\includeonly{chapter2}


then only chapter 2 will be processed. Unlike processing it as a completely separate document, the aux files of the other chapters will be read and so the chapter will still be numbered 2 and cross referenced to other chapters will work even if those chapters are omitted on this run.

-

Another, more general, way is to use the subfiles package. In your mainfile.tex you put the same content as you mentioned adding the subfiles package:

\usepackage{subfiles}


Then, in each chapterXX.tex you add something like:

\documentclass[mainfile]{subfiles}

\begin{document}
file content here ....
\end{document}


This will enable you to execute the file independently of your main file (i.e., you don't have to have the main file (or project) open to run the file.

(By the way, for TexnicCenter users, similar option mentioned for Texmaker above is available through the Project menu. Once you setup a project, hit F7 to build the whole project, or Ctrl+F7 to build the current file.)

-