71

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?

2

5 Answers 5

42

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.

0
93

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}% 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}
8
  • 2
    standalone looks like a very nice idea. I'll have to keep it in mind. Feb 17, 2011 at 18:56
  • 5
    @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! Feb 17, 2011 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, 2013 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. Nov 1, 2013 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, 2013 at 10:54
6

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.

5

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,

\begin{document}
%<*document>
    ...
%</document>
\end{document}

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>
\usepackage{catchfilebetweentags}

\begin{document}
    \ExecuteMetaData [file1.tex] {document}
    \ExecuteMetaData [file2.tex] {document}
\end{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.

\makeatletter
\def\CatchFBT@Fin@l#1[#2]{%
   \begingroup
      %\endlinechar\m@ne % <- this is the guilty party
      \makeatletter #2%
      \scantokens\expandafter{%
         \expandafter\CatchFBT@tok\expandafter{\the\CatchFBT@tok}}%
      \CatchFBT@IsAToken{#1}
         {\global#1\expandafter{\the\CatchFBT@tok}}
         {\xdef#1{\the\CatchFBT@tok}}%
      \ifx\CatchFBT@tok#1\else\global\CatchFBT@tok{}\fi
   \endgroup
}% \CatchFBT@Final
\makeatother

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

4

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .