# multiple files in LaTeX?

I currently use emacs to write and compile LaTeX files using the key combo: C-c C-f. Now this is all well and good, but now I'm working on a much bigger project that is divided into many chapters. My main file includes these chapters using \include{chp1} for example, and so I have to go back to the main file to use C-c C-f to compile everything.

So here's my question: When I am editing chp1.tex, is there any way to turn OFF the automatic recognition of LaTeX mode so that if I mistakenly hit C-c C-f, it doesn't freak out and spam me with pages and pages of LaTeX error messages? I'd like it to just tell me when I type C-c C-f in chp1.tex, "you stupid idiot, this is not the main file," rather than try to compile chp1 by itself.

• Do you have a Makefile for your bigger project? Dec 31, 2014 at 17:39
• Check out the AUCTeX manual on multifile documents. I suspect if you (setq-default TeX-master nil) as it suggests, AUCTeX will ask you for your master file before compiling.
– Dan
Dec 31, 2014 at 17:44
• You don't have to disable LaTeX mode: see Multifile Documents. If you set TeX-master as described in the manual you should be able to compile the whole document using C-c C-f from a buffer visiting a chapter file. Dec 31, 2014 at 17:44
• Oh, and are you sure you didn't mean C-c C-c (TeX-command-master)? The default binding for C-c C-f is TeX-font.
– Dan
Dec 31, 2014 at 17:46
• What am I, chopped liver? ;) @Constantine: could you convert your comment to an answer so it can be marked as answered?
– Dan
Dec 31, 2014 at 22:00

If you break latex doc into several files you can still work with the whole project just fine in emacs, all you need is to set the master file in each file you include. For this put the following at the top line of each file you include:

% -*- TeX-master: "dissertation.tex" -*-

(assuming your master file is called dissertation.tex). This way you get, for example, a working table of contents with C-c = and, I think, build with C-c C-f will work too. (personally I prefer including a build.bash or build.py file in every large project, so I don't use auctex-mode's build scripts)

One more nice binding for this work flow is C-c ^ which switches you to master file buffer.

• C-c C-f definitely works then. Another interesting binding with this workflow is C-c C-b, which will compile the current buffer only (but with the preamble from the master file, because *magic*). Jan 6, 2015 at 16:54
• Not really related to the question, but do you really need to include a build script in every project? Some people have spent some time developing complete such scripts, and they are distributed with texlive (if you haven't already tried them, you can look up latexmk, rubber or arara). Jan 6, 2015 at 16:56
• @T.Verron: well - yes. Because my build scripts do more then just latex build. For example I include svg files in latex (using Inkscape export to latex). So my build script converts, e.g., image.svg to image.pdf, if the later is outdated (script checks that). Also my build script builds everything in ./tmp dir of the project to keep master dir clean: it only contains pdf file of the build, master tex file and folders: tex, img, bib, tmp. Finally the whole procedure should be modified slightly if I use minted for source code coloring. Jan 6, 2015 at 17:25
• I would suggest to put TeX-master as a file local variable to the end of the file, instead of the beginning. The TeX-master-file-ask function (bound to C-c _ and used in many places in AUCTeX) searches only for the former format. Jan 6, 2015 at 17:39

Some other answers suggest modifying each file to include an emacs file variable to set the value of TeX-master, but I prefer to use emacs directory variables so that I don't have to modify each file. To do this, create a file called .dir-locals.el (see the link for caveats if you are on MS-DOS) in the directory where your TeX/LaTeX source files are located with the following contents (supposing your master document is called main.tex):

((LaTeX-mode . ((TeX-master . "main")))
(latex-mode . ((TeX-master . "main"))))

Then when you open any of the files in this directory where the mode is LaTeX-mode or latex-mode emacs will set the TeX-master variable for the buffer to "main".

Notes:

1. I have found that I have to restart emacs for the directory local variable to start taking effect and that just closing and re-opening a file in that directory is not sufficient.
2. There are lots of TeX/LaTeX modes, so if the above does not work, check what mode your file is and add a line specifying that mode (C-h v major-mode will show the mode). Careful with the parenthesis formatting.
3. A lazier (but less proper) alternative is to use the following contents for the .dir-locals.el file which will set the TeX-master variable for all files opened in the source directory whether they are TeX/LaTeX files or not.
((nil . ((TeX-master . "main"))))