# Compile multiple .tex files to one file

I am making a math formulary collection but the file is getting too big and too long!
So i was wondering if it is possible to make a .tex for for multiplication and one .tex file for division. And then make a third .tex file where i am importing the other two files and still be able to make TOC? and lets say each .tex files also have multiple sections and sub(sub)sections

In order to selectively include parts of your document and still get page numbers and table of contents right, you can use \include and \includeonly. Your main file should look like this:

\documentclass{whatever}
% put preamble here

% \includeonly{mult}
% \includeonly{div}
% \includeonly{mult,div}
\begin{document}
\tableofcontents
\include{mult}
\include{div}
\end{document}


The files mult.tex and div.tex should then only contain the corresponding text and formulas, but no preamble (no documentclass, no \begin{document}, etc.).

Note that \include always starts a new page. This is necessary in order to get consistent layout and page numbers. On the other hand, \input does not insert a new page, but then you do not get the right table of contents, and page numbers change when you comment out one of the files.

• Thanks for the anwser! I have edited the last line in the Question, i hope you have a great anwser for that – Daniel Guldberg Aaes Nov 4 '15 at 14:55
• No problem with that. Just put the \section and \subsection commands into your files mult.tex and div.tex. – jarauh Nov 4 '15 at 14:57
• A good read about \input vs. \include is tex.stackexchange.com/q/246/2975 – Martin Scharrer Nov 21 '18 at 11:47

Applying "divide and conquer" approach to your project -- by splitting your project into several smaller input files -- is a good practice.

In addition to this, it will be better if you can compile each smaller input file so you can see the result on which you focus. Compiling each smaller input file, of course, takes shorter time compared to compiling the project as a whole.

The following imitates your case. Consider you have 2 smaller input files called dif.tex and int.tex. Both are compilable as follows.

# dif.tex

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\section{Definition}
$\lim_{h\to 0}\frac{f(x+h)-f(x)}{h} = f'(x)$

\section{Partial differentiation}
$\textrm{d}(AB) = B\,\textrm{d}A +A\,\textrm{d}B$
\end{document}


# int.tex

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\section{Definite integral}
$\int_a^b f(x)\, \textrm{d}x = F(b) - F(a)$

\section{Partial integral}
$\int A\, \textrm{d}B = AB -\int B \, \textrm{d}A$
\end{document}


Now you can import both smaller files from your main input file (called main.tex) as follows. Note that we need to load docmute in the main.tex. The purpose of this package is to make \input only import contents between \begin{document} and \end{document}.

# main.tex

\documentclass{article}
\usepackage{docmute}
\usepackage{amsmath}

\begin{document}

\input{dif}
\input{int}

\end{document}


If your project is structured as follows,

.../main/main.tex
.../main/dif/dif.tex
.../main/int/int.tex


The main.tex has to be slightly modified as follows,

\documentclass{article}
\usepackage{docmute}
\usepackage{amsmath}

\begin{document}

\input{dif/dif}
\input{int/int}

\end{document}

• What if i have main.tex in a folder called main and then dif.tex in a subfolder named dif (/main/dif/dif.tex. How could I then import dif.? – Daniel Guldberg Aaes Nov 4 '15 at 21:29

You could organize your material in three separate files:

• File mult.tex: Just the body of the multiplication-related material, no preamble, no \begin{document}, and no \end{document}.

• File div.tex: Just the body of the division-related material, no preamble, no \begin{document}, and no \end{document}.

• File driver.tex, organized as follows:

\documentclass[<options, if any>]{article} % or report, or book, etc
... % preamble material, such as page size, etc
\begin{document}
\tableofcontents
\input mult
\clearpage
\input div
\end{document}


If you want to compile just the mult material, or just the div material, comment out one of the other \input statements, and be sure to recompile twice in order to propagate all changes (including the table of contents).

• Thanks for the anwser! I have edited the last line in the Question, i hope you have a great anwser for that – Daniel Guldberg Aaes Nov 4 '15 at 14:55
• @DanielGuldbergAaes - There would be no problem at all with the files mult.tex and div.tex containing \section, \subsection, etc directives. If, say, div.tex comes after mult.tex in the driver file, and if you want to compile just div.tex while keeping its main section number at 2, all you'd need to do is provide the instruction \addtocounter{section}{1} just ahead of \input div. – Mico Nov 4 '15 at 15:09