In Options->Root Document, TeXstudio allows the user to automatically detect the root document or manually set the root document. Then F5 will build & view from that root file. This is a pretty nice feature.

However, I want to be able to build & view the current file. This is how most other editors work but I have not been able to do it with TeXstudio.


This is an open feature request.

As a workaround, you can set the root of the subfiles to themselves. I.e. if you have mysubfile.tex, you can add the following entry to its top:

% !TeX root = mysubfile
  • Just in case anybody else forgot just like me, make sure "Detect Automatically" is active in Options->Root Document for this to work. – Batta Sep 27 '19 at 6:09

Assuming that you import those sub-documents with \input or \include with the preamble in the master document only, those separate files will not compile on their own. There are two possible solutions:

  1. Quick, one-time. Create a copy of the master file and remove all sub-documents that you do not need (or comment those inclusion lines). Compile. Voilà!

  2. If you really need to be able to compile separate sub-documents (article collection is a possible case), have a look at the standalone package. It allows you to compile both the master document and the separate input files as if they had been separate projects, with their preambles (if you choose the option shown below or not). Just use the class you want (say, article) for your root document with the corresponding package.

Use this in your master file (the sooner you include the standalone package, the better, according to its documentation):

\documentclass{article} % Root document

The last line is needed to correctly treat chapter-specific preambles. Then, for each chapter (or whatever division you use for the parts of your document), use

\documentclass[crop=false]{standalone} % Sub-document

This will allow you to compile both the master document and the subdocuments.

If you are not familiar with it, please try the example from this wonderful article (in the example, the extension of the subfiles is assumed to be .tex, so you can create them as an example and then copy the text and the preambles from your files to see whether it works).

  • I had compatibility issues with the standalone package, so I hacked together an alternative solution with a universal preamble file (including \documentclass) that checks whether it has already been run and responds accordingly. All my files run on their own, but TeXstudio only wants to run the (biggest, slowest) master file. – Jordan Jan 20 '16 at 20:42

Adapting the solution by faltfe in: In TeXstudio, how to compile only the subfile?

You can use the following macro to compile the current document and display it in the embedded pdf viewer:


function quote(filename){
    return '"' + filename + '"';

var filename = app.getCurrentFileName();
var dir = filename.substring(0, filename.lastIndexOf("/"));
var pdflatexcmd = '../MikTex/texmfs/install/miktex/bin/x64/pdflatex.exe  -src -interaction=nonstopmode '
var proc = system(pdflatexcmd + quote(filename), dir);

var pdfFilename = quote(filename.replace(/\.[^/.]+$/, ".pdf"));

app.runInternalCommand("txs:///view-pdf-internal" , "--embedded", pdfFilename);

You can register a keyboard shortcut for this macro, eg: Shift + F1 .

This allows you to compile and view the current selected document by just running this macro.

(For more info on defining / running macros, see: http://texstudio.sourceforge.net/manual/current/usermanual_en.html#SECTION33)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.