7

This is not really a practical question, but more of an academic one:

I use emacs to write my LaTeX files and in order to process multifile documents I include at the end of each file a local variable %%% TeX-master: "master" that tells emacs that when I compile this file I actually want to compile the file "master.tex".

Now I was just wondering if there is a way to do this in LaTeX something like \setmaster{master} or \includedin{master} that one can simply put in the begining of the file and it tells directly to the compiler it should start looking at the other file first.

  • I use arara for this: arara: making a rule to compile the main file from a chapter file There are a host of other automation tools, but imho, arara is king – cmhughes Jul 13 '13 at 9:42
  • @cmhughes: that looks quite nice, but it depends on an alternative compiler. my question was if LaTeX supports the feature natively. As far as my settings go they work pretty fine and I am not thinking of changing any time soon (hence the "academic question") – John C Jul 13 '13 at 9:54
6

This is possible, but with important limitations. Let's assume we have master.tex and secondary.tex.

First of all, how should \setmaster be defined? It's quite easy:

\makeatletter
\newcommand{\setmaster}[1]{%
   % Make all later \setmaster commands to be no-ops
   \global\let\setmaster\@gobble
   % Start reading the master file
   \input{#1}%
   % When it's finished, don't read from this file any more
   \endinput}
\makeatother

But where should \setmaster be defined? Either in the document itself or in a personalized format file. In the first case, surround the definition between \ifdefined\setmaster\else and \fi, because we don't want to read it when secondary.tex is input.

First limitation. Paths will be relative to the location of the secondary file, so all files must all be in the same directory: you can't have \input{../master} and then master.tex call \input{subdir/secondary}.

Second limitation. TeX sets the job name equal to the first input file, if not overridden from the command line with the option -jobname. So, without cooperation from the editor/front-end, you won't get master.log or master.pdf, but secondary.log. This is a built-in behavior of TeX and it can't be modified: as soon as TeX is started and opens a file for input (in this case secondary.tex), it opens the .log file with the name derived from it or from the -jobname command line option.

Thus such an approach is not better than stating what's the main file using the editor's or front-end's features. Many of them have this feature, so this should be the preferred method.

The arara methods outlined in the answers to arara: making a rule to compile the main file from a chapter file are worthy of more than a look, because they are independent from whatever editor/front-end is being used.

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.