Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
 1.1. Subheading1
 1.2. Subheading2
etc

File2:

1. Heading2
 1.1. Subheading1
 1.2. Subheading2
etc

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

1. Heading1
 1.1. Subheading1
 ...
2. Heading2
 2.1. Subheading2
 ...

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).

share|improve this question
    
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
add comment

migrated from stackoverflow.com Oct 24 '12 at 3:42

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

3 Answers

up vote 7 down vote accepted

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.

share|improve this answer
    
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
1  
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
1  
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
show 1 more comment

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:

enter image description here

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}
    \chapter{Heading1}
        \section{Subheading1}
        \section{Subheading2}
    \end{document}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
    \documentclass{report}
    \begin{document}
    \chapter{Heading2}
        \section{Subheading1}
        \section{Subheading2}
    \end{document}
\end{filecontents*}


\begin{document}
    \tableofcontents
    \input{file1}
    \input{file2}
\end{document}
share|improve this answer
add comment

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〉}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.