# How to include a document into another document?

I have two documents A and B. Both of them are separate documents. But document A also has to include document B.

Now if I use \include{B} i get the following error:

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.1 \documentclass
[11pt]{article}
?


So how can I force to include document B with the "style" and "template" of document A?

-
 Similar questions: tex.stackexchange.com/q/11221/2975 and tex.stackexchange.com/q/8198/2975. – Martin Scharrer♦ Feb 17 '11 at 16:11

Cut the content (the part between \begin{document}...\end{document} of B.tex into a new file B-content.tex.

Change B.tex to be:

\documentclass{...}
\begin{document}
\include{B-content}
\end{document}


Then put \include{B-content} into A.tex.

-
 So easy but useful. Thanks! – RoflcoptrException Feb 17 '11 at 15:55

You can use the standalone, docmute or subfiles package to make LaTeX ignore the second preamble.

Simply load the standalone package in the main file and \input or \include the document. This is a good way if the to-be-included documents just holds a picture which should also be compiled standalone. In this case having main files for every picture file would be annoying.

% A.tex
\documentclass{article}
\usepackage{standalone}
\begin{document}
% ...
\input{B-content}% or \include
% ...
\end{document}

% B.tex (for normal text)
\documentclass{article}
\begin{document}
\end{document}


or if B should hold some diagram only (note the different class):

% B.tex
\documentclass{standalone}
\begin{document}
\end{document}

-
standalone looks like a very nice idea. I'll have to keep it in mind. – Matthew Leingang Feb 17 '11 at 18:56
@Matthew: Thanks, I wrote it because I have a lot of TikZ pictures and I hated the long re-compiling runs during the creation of the more complicated ones when they are inside a document. They are used multiple times across my papers, presentations and thesis anyway. Having extra main files for each was to cumbersome for me. Have a look at the options: you can collect all the "sub-preambles" automatically! – Martin Scharrer Feb 17 '11 at 19:02

You can try using the combine class but be warned this is not what LaTeX is designed for!

An alternative that I haven't tried is to use the newclude package and write

\includedoc{fileB.tex}


This latter approach assumes that all of the packages, etc., you need are loaded by the first file.

-

As far as I know, include simply inserts the text wherever it is used. So you can't have a preamble in document B. At a quick glance, I would create a wrapper document C and use \include{B} in both after editing B so that it only contains your desired output text

-