# How do I compile standalone files in TeXStudio?

I get some unexpected behavior when I try to "Build & View" or "Compile" the standalone file mysubfile.tex in TeXstudio.

The following results in expected behavior:

1. Open fresh session of TeXstudio
2. Open only mysubfile.tex (which resides in a folder containing only this file and mymainfile.tex)
3. Build & View (or Compile)
4. Message output produced:

1. and mysubfile.pdf produced, which is just the cropped text "Some subfile text".

as expected.

But the following results in unexpected behavior:

If I then open mymainfile.tex in TeXstudio, re-select the mysubfile.tex window, and Build & View, then:

mymainfile.pdf:

I get the same output even if I then close mymainfile.tex in TeXstudio, get rid of all the produced files in my project folder (so that only mymainfile.tex and mysubfile.tex remain), and Build & View mysubfile.tex. What!? Somehow TeXStudio is aware that this is not a fresh session.

I looked to the TeXstudio command configurations

Q1: How does TeXstudio decide which filename (mymainfile or mysubfile) is passed to pdflatex through %

Q2: How can I fix this so that when I Build & View with the mysubfile.tex window selected, mysubfile is passed to pdflatex rather than mymainfile?

mymainfile.tex

\documentclass{article}
\usepackage{standalone}
\begin{document}
My main file text.
\input{mysubfile.tex}
\end{document}


mysubfile.tex

\documentclass{standalone}
\begin{document}
Some subfile text
\end{document}

• Is there an option which tells it not to automatically detect a 'master' file, for example? The terminology might differ - that's what Kile calls it. – cfr Oct 9 '14 at 2:56
• @cfr: Awesome. Once I searched using the 'master' terminology you mentioned I found this comment on SourceForge. So all I need to do: Select subfile.tex > ALT + O > Select "Define Current Document as 'Master Document'". – lowndrul Oct 9 '14 at 12:14

As of version 2.8.10, accelerator keys for setting the explicit root document do no longer work.

However, TeXStudio supports a magic comment (at the beginning of the document)

% !TeX root = FILENAME.tex


that allows setting file that is used when Options > Root Document > Detect automatically is enabled.

So basically, the root-document resolution order is

1. The explicitly set file, as set by Options > Root Document > Set Current Document As Explicit Root
2. The file indicated by the % !TeX root magic comment.
3. The root file detected from analyzing currently opened files.

Select subfile.tex > ALT + O + M > Build & View.