I am not asking about \include or \input here. I know all about these.

Here is the setup. I'd like to be able to build as standalone one of my HW reports which sits in its own folder. Then I also like to include this latex file and others in the main latex file in some parent folder, for my whole course to assemble everything into one document.

The problem is that to build the one HW on its own, I need to have it as a complete separate Latex document. So, when it comes to importing it into the main document, I have to go comment out all the preamble from each HW document otherwise one gets an error.

Note that I have to use the import package for this, and not the \include, since the import package handles relatives paths correctly. So I can't use things like \includeonly{A/a} since a.tex include images using relative paths in them, and I'll get an error about image not find if I just use \input or \include.

I have to use \subimport.

Better to give a simple example than all this rambling. Given this layout


a.tex is


and main.tex is

\subimport{A/}{a}  %notice that images included by a.tex are OK now

I can build a.tex ok. But to build main.tex, I have to comment a.tex as follows


And to use a.tex again on its own, go back and uncomment the lines. This is getting tiring.

It will be nice if there was an option during an import to tell it to strip out all the latex preamble as it imports the file!

Any suggestions how to do this without this manual step?

I really do not want to collapse all my course tree into one folder just to do this. I like to keep each project and HW in its own folder so I can build each as a separate document, but I like to also be able to build the whole tree documents into one document.

3 Answers 3


The standalone package got written to do exactly that, i.e. remove preambles from sub-files. For this it redefines \documentclass (after \begin{document}) to strip everything until \begin{document} and ignore any additional document environments. The package works fine without the accompanying standalone class. However, I didn't explicitly tested it with the import package yet, but it should work.

  • wonderful package! Thanks so much for making it. I tried it and it works like a charm. I'll update the question will example using it for future reference.
    – Nasser
    Commented Jan 22, 2013 at 7:17
  • 1
    @Nasser: It would be better to keep the question in its original form and instead post your final solution as a self-answer. This way the question and solution is cleanly separated, which will help other people with the same issue more. Commented Jan 22, 2013 at 10:32
  • There is a small issue that I just found about. For some reason tex4ht gives an error when it tries to process main.tex in the example I have above. I'll post what I tried and the error message. I wonder if you could please look at it. It might be a tex4ht own error and not. I was hoping to also be able to run these latex files by tex4ht to generate HTML. Latex2html does not even support the import package, so that is out of question to use.
    – Nasser
    Commented Jan 22, 2013 at 10:46
  • There is also the subfiles package - it would be nice if someone summarized the differences.
    – marczellm
    Commented Jan 22, 2013 at 11:30
  • @marczellm, thanks for the link to subfiles. I just tried it on the above example I have with main.tex and a.tex. Unfortunately it uses input on the subfiles, which means if a child document includes an image relative to it, then when building the parent document will give an error image not found. The advantage of import package is that it handles relative paths correctly. I'll add an example in a new update FYI, so it is all self contained.
    – Nasser
    Commented Jan 22, 2013 at 11:58

I spent some time exploring the various methods of doing his kind of thing last year and eventually came to the conclusion that all the packages for doing this are fundamentally taking the wrong approach and you're better off not using them at all.

Instead, create entirely separate "content" files from "framework" files so that no file contains both text you want to appear in your document and preamble. So in the simplest case, you might do something like this:




Ipsus wotsisus thingyus \emph{bimble}

So content files will only work when included in framework files and all the display options get set from the the framework file If you then separate your preamble to a new file you can include you can include it in different framework files collecting particular chapters, etc without any difficulty. This approach has several advantages:

  1. It doesn't require any special packages to be juggled.
  2. It entirely separates your content from it's display options; putting your content into a new setup is as simple as creating a new framework file.
  3. You don't have to repeat yourself at the start of every sub-file.

And, in your case, you can use \import as you need. The disadvantage is that you have more files overall. I can't say I see much problem with that YMMV.

  • 1
    This is a good answer also. I just saw it. I will try it. Yes, there is always this choice, between keeping things simpler, at the cost of may be additional files. But disk space is cheap, and I use Make anyway, so all this can be automated. Thanks for the idea.
    – Nasser
    Commented Oct 31, 2013 at 6:26

Just simply delete the documentclass{} etc. element from your sub latex files. If you are working on nested files on latex you do not need them in your sub latex files but in your main latex file.

  • Welcome to TeX.SE!
    – Mensch
    Commented Dec 23, 2020 at 10:06
  • have you ever tried? Commented Jan 20, 2021 at 9:04

You must log in to answer this question.

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