[Tex/LaTex] 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


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.

Best Answer

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.