I have a main latex document, and many other files included with \input{...}, e.g. by chapter.

Often, I just work on a subfile and my idea is now, that I want to compile the main document if I start the compilation of the subfile.

A short example: main document



some text...

So I imagine the following (in pseudo-code) to be in automatic_mainfile_compilation.sty

if code == NOT inserted vie \input
  \write18{pdflatex main_document}
  stop compilation with return value of line before 

rest of subdocument

Stopping dependent on the called compilation process shouldn't be a problem, but can I somehow recognize during compilation, if I am in an 'inputted' file, and the skip some wirte18{pdflatex...}-command?

  • 5
    Erh, isn't that more a job of the editor? Most editors these days support the concept of a master document to be compiled whenever you ask to compile, regardless of which subfile you are in
    – daleif
    Oct 23, 2015 at 10:17
  • 1
    All editors are different. I would not like to receive a file that suddenly starts compiling it self. I would not recommend spending more time on this.
    – daleif
    Oct 23, 2015 at 10:20
  • 2
    You can't use \usepackage after \begin{document}. Beside this: Your method will only work with --shell-escape, which means that you would have to setup your editor to use it. Setting the main file is easier. Oct 23, 2015 at 10:58
  • 2
    Well I wouldn't activate --shell-escape by default. And you don't need to do complicated tests for \begin{document}: It is enough to test if \jobname is main. But you will not be able to insert your code with \usepackage in the subfiles, you must use \input. Oct 23, 2015 at 12:17
  • 1
    why not use \include ?(processing single sections is exactly what \include is there to support) Oct 23, 2015 at 20:30

1 Answer 1


The following code is fine for me now:

\def\myjobb{main} % name of main tex file

  \immediate\write18{cd .. && pdflatex --shell-escape \myjobb} % go up one folder, as file is in subfolder for subfiles...
  \immediate\write18{ pidof mupdf-x11 | xargs kill -s SIGHUP} % reload pdf

Or even simpler: When the subfiles are in an subfolder, the different cases can be easily separated by \input{somefile} which exists two times, once as an empty file in the folder of the main tex file, and once to start compilation in the subfolder...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .