How to compile from a subchapter or how to set root file?

Please use the form % !TeX root = ../main.tex

For details see the TeXstudio user manual.

Background information on % !TeX

These 'magic' comments were first introduced by TeXShop. Afterwards, other editors started to adopt them, including TeXworks, Texmaker and TeXstudio. There's no official standard. So capabilities, and tolerance towards capitalization and spaces may be different among the editors. The above form is the original one and should be accepted by all editors.

Additional hint

TeXstudio has an integrated root detection. If you have main.tex open and sub.tex included in main. It automatically detects that main.tex is the root document and thus compiles that even if the editor has focus on sub.tex.

It's still good practice to set % !TeX root because then everything works even without an open root document. Additionally, your TeX files are more portable since AFAIK other editors do not support automatic root detection.

For further details see the user manual section on multi file documents.


I was looking for an answer for quite some time but couldn't find an answer on the internet. Because of that i thought it would be good to post this problem with the solution i found:


The solution for this problem is quite simple. The mentioned root command doesn't work in TexStudio but you can set a root file manually:

  1. Open the you root/main file
  2. TexStudio menu: options
  3. Root document
  4. Define Current Document as 'Master Document'

I hope this does help someone else.


If there is a alternative to the "%!TEX root =..." command for TexStudio then please feel free to write an answer!