I am writing five or so texts which serve as course notes. I have one master file Style.tex into which I include the various bodies of these course notes. The reason I do it like this is because it's easier to make sure I am using the same style and notation across several documents. However, I have to edit Style.tex to specify which body text I would like to include. It would be nice if I could compile all notes simultaneously and generate pdfs for each set of notes with one command.

Does anyone have suggestions for how to do this?

  • 2
    Welcome to TeX.SX! TeX is able to produce only one PDF for compilation; you probably need some sort of script for this task. What operating system are you using?
    – egreg
    Jul 21 '13 at 10:07
  • 2
    The standard way to make sure a collection of documents share a common style is to start them all \documentclass{mylocalclass}\begin{document}.....\end{document} and put all the shared code in the document class file. Jul 21 '13 at 10:11
  • 1
    Following up on David's comment, if you don't feel comfortable tackling your own local class file, you can put the common code into a style file and \usepackage{mylocalstyle}. Jul 23 '13 at 13:01

I think that your present approach in the reverse of what must be done. If you don't want the hassle to write your own class (as suggested by David Carlisle) or even package, which is somehow tricky, you have the standard solution as follows :

First you put in a mystyle.sty file ( without \documentclass[]{} nor \begin{document}.....\end{document} ) all the \usepackage{} and manual settings that must be shared (and only this).

Secondly you reorganize your courses files as regular tex files (one per course) like this :

 \documentclass[<options>]{report} % or another class you prefer
 % here put settings or info specfc to to this course, as \title{} aso>
   <body text>

You then can compile them while sharing all the content of mystyle.sty, and need no never multi-editing. Furthermore, if you change something in this mystyle.sty, you will be able to transfer the change by a simple recompilation of all the ourses file without edit them (a script could be useful for this last step if you have to many courses...)

EDIT a very simple batch file for DOS/Windows would be:

@echo off
FOR %%C IN ("*.tex") do (
echo "compiling"  %%~C
pdflatex -interaction=nonstopmode -shell-escape %%~dpnC > all.log
FOR %%A IN ("aux","out","nav","log","toc","thm") do IF EXIST %%~dpnC.%%A (del %%~dpnC.%%A) 

of course :

  • "*.tex" could be refined in eg "course*.tex"
  • the pdflatex line could be customized according to your aim, o better replaced by another script which builds all including BiBTeX biblio, indexes aso.
  • the list of deletion can be extended in a straightforward way.
  • Why not also include the documentclass in mystyle.tex?
    – Aditya
    Jul 21 '13 at 16:46
  • @ Aditya : just a question of taste ! To ensure by inspection that you have a regular latex file with the mandatory \documentclass{}, \begin{document} and \end{document}. The key point was to reverse the logic of the inputs.
    – Jhor
    Jul 21 '13 at 17:06
  • @psychoticpsychiatrist I apologize, I have read your post to quickly: even if the naming 1.tex aso is not a good idea your script does create the file as long as mine assumes that they already exist.
    – Jhor
    Jul 23 '13 at 12:56

I just made a batch script for this myself (for Windows).

@ECHO off
FOR /L %%X IN (1,1,5) DO (
IF EXIST %%X.tex (
TYPE start.tex > temp.tex
TYPE %%X.tex >> temp.tex
TYPE stop.tex >>temp.tex
pdflatex -synctex=0 -interaction=nonstopmode -shell-escape -jobname=%%X temp
FOR /L %%X IN (1,1,5) DO ( DEL %%X.aux & DEL %%X.log & DEL %%X.out)

Place 1.tex, 2.tex, ... , 5.tex (the name of your files) in a directory with this script (named whatever.bat). Make a start.tex file (in the same directory) which looks like

some preamble

and a stop.tex file (in the same directory) containing


What the script does is, checks if a file 1.tex exists, and if so, writes start.tex to temp.tex, include the text in 1.tex in temp.tex, writes stop.tex to temp.tex and compiles temp.tex with the name 1.pdf. Then moves on to 2.tex, and so on.

The line before the PAUSE command removes auxiliary files. You may want to remove this line.

You might also want to change the pdflatex options.

If you get more documents, change the (or both) for loop to (start,step,stop), that is (1,1,10) for 10 documents.

Please not that this is my first batch script ever and i strongly recommend you to take copies of what you've written before you use it. If you're on Linux, perhaps someone can write a bash script (I don't have access to Linux right now).

Edit: Please use jhor's one instead. Mine is, as pointed out, a beginner's one. For me, the numbering was important (solution files in a textbook, attached in pdf to specific questions which could then be thrown around at will), hence the simple for-loop which gets the work done.


Another option is to define the content file to be compiled on the command line.

So, if the file below is saved as Style.tex, then to compile the body of DocumentA you would use:

pdflatex "\def\MyFile{DocumentA}\input{Style.tex}"

This eliminates any need to change the Style.tex file.




%% Your custom style settings, either included from a .sty
%% or defined here.

    \PackageError{Style.tex}{"MyFile" was not defined.}{}

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.