Tell me more ×
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 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?

share|improve this question

4 Answers

up vote 9 down vote accepted

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{...}
% your preamble here
\begin{document}
\include{B-content}
\end{document}

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

share|improve this answer
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}
% your preamble here
\begin{document}
% ...
\input{B-content}% or \include
% ...
\end{document}
% B.tex (for normal text)
\documentclass{article}
% your preamble here
\begin{document}
% your B content here
\end{document}

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

% B.tex
\documentclass{standalone}
% your preamble here
\begin{document}
% your diagram code here
\end{document}
share|improve this answer
standalone looks like a very nice idea. I'll have to keep it in mind. – Matthew Leingang Feb 17 '11 at 18:56
2  
@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

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

share|improve this answer

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.

share|improve this answer

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.