[Tex/LaTex] How to compile standalone files in TeXStudio

standalonetexstudio

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:

enter image description here

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

enter image description here

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:

enter image description here

mymainfile.pdf:

enter image description here

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

enter image description here

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}

Best Answer

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.
Related Question