TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

Possible Duplicate:
Make a .tex file that combines complete .tex documents in subdirectories

I have a couple of files, which were initially intended as stand-alone documents. But now I wish to compile them in a single document. The problem is that each of them has its own specific packages, titles, etc and the \include method isn't so straight forward for such documents. (I have to include the same list of packages in the main file, and comment them all out in the sub-document, which is a long manual procedure.)

What is the best method to compile them all in one, besides externally merging PDFs?

share|improve this question

marked as duplicate by lockstep, Werner, Seamus, egreg, Joseph Wright Dec 4 '11 at 14:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@lockstep: It sounds more to me that the OP has small documents, while the standalone package is more for smaller things like picture environments. This looks more like a case for using the combine class. – Martin Scharrer Dec 4 '11 at 13:00
up vote 3 down vote accepted

I have been using the {standalone} package exactly for this purpose. The main files should use:


or load the standalone package near the beginning of the preamble. The complete preamble needed by the sub files should be loaded by the main document.

In the example below, I have used the filecontents package just to be able to wrap the sub file into one code sample. So, the file subfile.tex here is a sub file, in this case it is composed of a tikz figure. This file can be compiled on its own. The main file is loading:


and then simply uses \input{subfile} to import the sub file.





    \coordinate (EllipseOrigin) at (0,0);

    \draw [blue, thin, ->] (-5,0) -- (5,0) node [right] {$x$};
    \draw [blue, thin, ->] (0,-4) -- (0,4) node [above] {$y$};

    \draw [red, ultra thick]% Graph Ellipse
        (EllipseOrigin) ellipse [x radius=\XRadius,y radius=\YRadius];



There is also the subpreambles package option to the standalone class which allows you to use the preamble form the sub files. So if you can not have one preamble in the main file for all your subfiles, perhaps this might be a useful option.

There is also the combine package, which also lets you combine separate documents into one document.

share|improve this answer

In addition to the linked question, the subfiles package provides a similar functionality. From the package README:

Using subfiles the user can handle multi-file projects more comfortably making it possible to both process the subsidiary files by themselves and to process the main file that includes them, without making any changes to either.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.