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


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


  • 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. Commented Jan 18, 2013 at 20:53

4 Answers 4


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.

  • 2
    Awesome. I have a feeling that's by far the easier of my two options...
    – enderland
    Commented Jan 18, 2013 at 20:33
  • 8
    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
    Commented May 20, 2013 at 15:08
  • 7
    My life just became so much more efficient. You, sir or madame, are truly wonderful. Commented Jan 17, 2014 at 19:01
  • 3
    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
    Commented Jul 8, 2014 at 8:27
  • 3
    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 Commented Feb 4, 2016 at 6:37

For people coming here searching for a solution in TexWorks, add this at the very top of your subdocuments:

% !TeX root = path/main_file.tex

Then TexWork will typeset your main document instead of the currently open file.

  • This works on Texstudio as well.
    – Mohit
    Commented Mar 30, 2018 at 11:40

You can just process your main file but add


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:


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


  file content here ....

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.)

You must log in to answer this question.

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