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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question
if you want document B always to compile with document A as the outside, use %!TEX root = docA.tex instead of the preamble of document B tex.stackexchange.com/questions/148649/… – honi Apr 19 '15 at 19:08
up vote 25 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:

% your preamble here

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
% your preamble here
% ...
\input{B}% or \include
% ...
% B.tex (for normal text)
% your preamble here
% your B content here

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

% B.tex
% your preamble here
% your diagram code here
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
@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
@Martin 1. Is B-content in your code a typo for B? 2. I am unable to extract your simple examples here from your package documentation. I would recommend clarifying your package documentation accordingly. 3. I cannot specify a path with \include, whereas it works fine with \input. Is that behavior intentional? – Sverre Oct 31 '13 at 15:17
@Sverre: 1. Yes, that was wrong. I corrected it above. 2. I'm planning to rework the manual already. 3. \include does a lot more than \input and uses the given filename several times for e.g. creating an .aux file. Due to security reasons TeX doesn't allow .. in relative file paths and not absolute file paths at all. I guess one of these two things cause trouble in your case. – Martin Scharrer Nov 1 '13 at 9:26
@Martin: What do you mean by ..? And "TeX doesn't allow absolute file paths at all"? I use absolute file paths for many things in my preamble. – Sverre Nov 1 '13 at 10:54

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


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

share|improve this answer

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

Here is probably a non-conventional way of doing this. I was trying to combine a few files while still hoping to be able to compile each file individually. I used the catchfilebetweentags package to get it done.

Let says I have file1.tex and file2.tex, which I only want to include the part lies between \begin{document} and \end{document}. I labelled it with tag name of document.

In file1.tex and file2.tex,


For clarity, %<*tagname> and %</tagname> is the way to tag the content to be used in the catchfilebetweentags package.

So in my main.tex, I want to include the content in file1.tex and file2.tex.

<remember to add preambles that are needed>

    \ExecuteMetaData [file1.tex] {document}
    \ExecuteMetaData [file2.tex] {document}

Voila! Here you go! You can still compile each of the file individually.

There is one problem with cathcfilebetweentags as mentioned in \input only part of a file using catchfilebetweentags package. The line break is not recognised when you are having an empty line to indicate line break.

Thank to egreg for providing the solution here. Basically you can just add this to your preamble.

      %\endlinechar\m@ne % <- this is the guilty party
      \makeatletter #2%
}% \CatchFBT@Final

If you desire more explanation on this, please check out on \input only part of a file using catchfilebetweentags package.

share|improve this answer

Your Answer


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.