When using \include{file}, if the including file is a complete LaTeX document, with documentclass{}, \begin{document}, etc., it generates an error. It doesn't generate the error if I remove everything except the content (those between \begin{document} and \end{document}).

But isn't the very point of \include command to work separately from a long document? What's the point if I can't compile the including file separately (having no preamble, etc)?

Is \include the best option for working on, say, a chapter of a long document separately?

-

migrated from stackoverflow.comJun 12 '11 at 13:27

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

@ashpool --- you might want to look at the standalone package, which can cause TeX to skip extra preambles in included files. –  Ian Thompson Jun 12 '11 at 15:29

But isn't the very point of \include command to work separately from a long document?

True, but it’s not the point of the \include command to include complete documents, as you’re trying to do; rather, it works on fragments of a document.

What's the point if I can't compile the including file separately (having no preamble, etc)?

The point isn’t that you have complete documents. If you want to compile just some chapters but not others, you can use the \includeonly command to specify which chapters to include, and which not to include.

Is \include the best option for working on, say, a chapter of a long document separately?

It’s the best option that’s ready-made. You can however create your own macro that may work better for your specific circumstance (that’s what I have done for my master thesis).

For your specific use-case you could create a new macro that disables the \documentclass, \usepackage and \begin/end{document} commands. For instance, this might look as follows (untested!):

\newcommand\includedocument[1]{%
\begingroup
\renewcommand\documentclass[2][]{}
\renewcommand\usepackage[2][]{}
\let\document\relax
\let\enddocument\relax
\include{#1}
\endgroup}

-

I have been using the {standalone} package exactly for this purpose. It basically provides the functionality that Konrad mentioned. The standalone files should use:

\documentclass[preview=false]{standalone}


and the main files which include this standalone file will need to have the complete preamble including \usepackage{standalone}.

Martin, the package author provides a good example here.

-
Just beat me to it! Definitely the best tool for the job. –  Brent.Longborough Jun 12 '11 at 18:06

\includes are intended to break things into separate pieces so that you don't have to have everything in a whole file, but they are a direct "drop the entire contents of this file in place right here, right now" statement. It won't try and figure out that you only wanted part of the other file.

So, to accomplish what you're looking for you have 2 choices:

1. Put all the includes in a master file in one spot, and simply comment out all the other includes but the chapter you're working on.
2. Make another top-level document to use to include only the chapter in question.
3. Go ahead and still compile the whole thing. It's probably not that much longer time-wise to do so. Includes still let you do organization, making it easier to find the content you want to be editing currently.
-