# In TeXstudio, how to compile only the subfile?

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!

• You can just compile the file with the standalone class. In TeXstudio select the tab of this file and press the compile button (or F6) Is this what you want? – faltfe Sep 12 '18 at 13:23
• you can use the magic comment %! TeX root: sub if your subfile name is sub.tex, then if you press compile while you're in sub.tex, it will compile the subfile instead of the main file. – Troy Sep 12 '18 at 13:34
• None of these suggestions work in TeXstudio it seems, at least not once a root file has been picked already. I can change the root file manually by clicking on "Options->Root Document->Set root document explicitly" to the one I want to compile right then and there, which is a bit excessive when jumping around between files/documents. @faltfe – pi223871 Sep 13 '18 at 6:59
• @pi223871 I always used the automatic detection of the root file so far. That's probably the reason why there is no problem with jumping between different files for me. But if you change a subfile why wouldn't you change the main file as well, when it includes the subfile? – faltfe Sep 13 '18 at 7:13
• @pi223871 I think you don't mean a standalone. Could it be, that you only use a seccond .tex file without \documentclass{standalone} and includes it into the main document with \include{filename}. Maybe you can show us a minimal example of the file you want to compile. – Ahrtaler Sep 13 '18 at 11:46

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.

• +1 Thanks for your answer! Off-topic: As you are our resident texstudio expert, do you know where %BEGIN_FOLD and %END_FOLD are documented? I was asked this in a comment tex.stackexchange.com/questions/450793/… but could not answer. I searched the texstudio user manual, but cannot find it there. – user36296 Sep 15 '18 at 14:37
• @samcarter It's defined in tex.qnfa, it's alluded to in the user manual, but not explicitly documented (that code folding is controlled by qnfa files). see here; also lines 23 and 24. – Troy Sep 15 '18 at 14:40
• Fantastic! This is very helpful - thanks a lot! – user36296 Sep 15 '18 at 14:44

There is some kind of workaround one can use. You can write a small macro to run a single file using the subfiles class in TeXstudio. Actually it the macro doesn't care about the documentclass.

Suppose you have the following two files main-document.tex

\documentclass{article}
\usepackage{subfiles}
\begin{document}
My main file text.
\subfile{subfile-document}
\end{document}


and subfile-document.tex

\documentclass[main-document.tex]{subfiles}
\begin{document}
Some subfile text
\end{document}


Now you can define a macro in TeXstudio using Script.

%SCRIPT
var filename = app.getCurrentFileName();
var dir = filename.substring(0, filename.lastIndexOf("/"));
var proc = system("pdflatex " + filename, dir);


The macro does the following:

• Get the current select filename
• Extract the directory path
• Call pdflatex <filename> inside the cmd and passes the correkt directory to the command line

TeXstudio uses keybindings for macros by default Shift + F1 but you are able to remap them. The number of the Fx key depend on the number of macros you have defined.

Now you can run the only the subfile by

1. Select the subfile-document.tex tab
2. Press Shift + F1 to run the macro. (TeXstudio might ask you for futherer privileges)

EDIT: I've created an issue asking about how to load a pdf file using a macro script on https://github.com/texstudio-org/

• Ok, this works, it compiles the file! Now I need another command in the macro so that the pdf shows up in the pdf-viewer in TeXstudio. Just pressing F7 will pull up the last document compiled by TeXstudio itself. Any ideas? – pi223871 Sep 13 '18 at 13:50
• For the moment I just could find the object pdfs in the docs which lists all opened pdf viewers. But I can't manage to show the created pdf so far. – faltfe Sep 13 '18 at 16:53