# Creating complex documents from separate files

I have multiple documents (file1.tex, file2.tex, etc.) and one main file (let's call it main.tex).

I would like to build a complex document that contains all file_i.tex files. I tried this (according to some online guide):

\documentstyle{report}
...
\begin{document}
\input{file1}
\input{file2}
...
\end{document}


I tried include command too, but I keep getting "LaTeX Error: Can be used only in preamble." What am I doing wrong?

Bonus question: I have headings structure in all my files like this:

File1:

1. Heading1
etc


File2:

1. Heading2
etc


When I build my document as described below. I want my heading to be updated like this:

1. Heading1
...
...


And then generate index. Is there some easy way how to do this in LaTeX (I believe there is, when it was designed to do this but I cant seem to figure out the right command/structure).

-

## migrated from stackoverflow.comOct 24 '12 at 3:42

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID). –  Qrrbrbirlbel Oct 24 '12 at 3:44

If you input a file, that other file must not contain a preamble or other header/footer material, because it will be included verbatim at the point of \input.

Therefore, you should strip your file_i documents of their TeX header and footer, and leave only the content itself.

-
Thanks, that works. Just one more thing - in which file can I use "usepackage" commands? It works when I delete them all, but wherever I put them, I get an error. thx –  Smajl Oct 23 '12 at 21:33
Only in the main document. –  nneonneo Oct 23 '12 at 21:34
Error: LaTeX2e command \usepackage in LaTeX 2.09 document (this is what I get when I put it in the main text file) –  Smajl Oct 23 '12 at 21:36
Use \documentclass instead of \documentstyle. The latter is an older form which triggers a compatibility mode in LaTeX. –  nneonneo Oct 23 '12 at 21:39
Thank you - everything works now! –  Smajl Oct 23 '12 at 21:45

You actually can use \input on files that are complete compilable LaTeX files (with a preamble) if you use the standalone package in the main document. There are numerous examples on this site, but below I have used the filecontents package to make a self contained example that shows how this can be used.

To answer your other question, assuming that you are referring to a table of contents rather than an index, you can do this by including \tableofcontents in the main document. If you want to also have hyper links in the table of contents you need to also include the hyperref package.

The code below yields the desired structure with click able links:

## Note:

• This does require two runs. First one to generate the file containing the necessary information for the \tableofcontents, and the second to actually produce the Table of Contents.

## Code:

\documentclass{report}
\usepackage{standalone}
\usepackage{hyperref}

%\usepackage{filecontents}%
\begin{filecontents*}{file1.tex}
\documentclass{report}
\begin{document}
\end{document}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
\documentclass{report}
\begin{document}
\end{document}
\end{filecontents*}

\begin{document}
\tableofcontents
\input{file1}
\input{file2}
\end{document}

-

As nneonneo already said, there is no need for preambles in included files. In case you want to realize more complex document structures with input or include commands on two or more levels you can also consider to use the import package (CTAN) to include files using a relative path.

• \import{〈full_path〉}{〈file〉}
• \subimport{〈path_extension〉}{〈file〉}
• \includefrom{〈full_path〉}{〈file〉}
• \subincludefrom{〈path_extension〉}{〈file〉}
-