# Compilation of main document, if subdocuments are compiled

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

\documentclass{minimal}
\begin{document}
\input{subfile}
\end{document}


subfile.tex

\usepackage{automatic_mainfile_compilation}
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
end

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?

• 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 '15 at 10:17
• 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 '15 at 10:20
• 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. – Ulrike Fischer Oct 23 '15 at 10:58
• 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. – Ulrike Fischer Oct 23 '15 at 12:17
• why not use \include ?(processing single sections is exactly what \include is there to support) – David Carlisle Oct 23 '15 at 20:30

The following code is fine for me now:

\def\myjobb{main} % name of main tex file
\edef\myjob{\meaning\myjobb}
\edef\tmpjob{\jobname}
\edef\tmpjob{\meaning\tmpjob}

\ifx\myjob\tmpjob
\else
\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
\stop
\fi


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