[Tex/LaTex] In TeXstudio, how to compile only the subfile

subfilestexstudio

There exist several questions along the same line as mine here, but none of them have been answered:
How do I compile standalone files in TeXStudio?
Texstudio and subfiles package
Problems when compiling subfiles (with the subfiles package)

How do I get only my subfile to compile in TeXstudio once it has detected what the root file is. Currently, it will always compile the root file, instead of just the subfile, which takes away the actual function of the subfiles package! Annoying!

Best Answer

You can use a magic comment called %!TeX root at the beginning of your subfile.tex to override the default root detection by TXS. This way, when you are in subfile.tex and press compile, TXS will treat subfile.tex as root and compile that instead of main.tex, as required.

As an example, it might look something like:

subfile.tex

%!TeX root = subfile
\documentclass[main.tex]{subfiles}
\begin{document}
    Hello world.
\end{document}

This was also covered in the TXS user manual.

Related Question