0

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?

crateane
  • 2,217
  • 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 '15 at 10:17
  • Yes, but if I change the editor, this would also work... – crateane Oct 23 '15 at 10:19
  • 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 '15 at 10:20
  • That's my point, all editors are different ;) So my approach could work with any editor... I just would make a sty-file, which is included in all subfiles. – crateane Oct 23 '15 at 10:54
  • 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. – Ulrike Fischer Oct 23 '15 at 10:58
  • @UlrikeFischer I need --shell-escape anyway... And recognizing if there has been \begin{document} is no problem: http://tex.stackexchange.com/a/16298/44467 But maybe this already resolves my problem, because then I don't need to know anymore, if the current file is loaded by \input or not... – crateane Oct 23 '15 at 12:00
  • 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. – Ulrike Fischer Oct 23 '15 at 12:17
  • @UlrikeFischer Of course you are right, I have to use \input... and the test seems to be quite easy now, \jobname seems to be the right thing... – crateane Oct 23 '15 at 12:28
  • 1
    why not use \include ?(processing single sections is exactly what \include is there to support) – David Carlisle Oct 23 '15 at 20:30

1 Answers1

0

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

crateane
  • 2,217