In other words, Can I put the package dependencies of an included file inside the file itself? Or I must use the main file for that?

I am in a situation in which I include different slides in a main.tex file. Different slides use commands using different packages.

\include{slide1} %slide1.tex needs apackage
\include{slide2} %slide2.tex needs bpackage

Can I write slide1.tex and slide2.tex in such a way that main.tex doesn't need to know the packages needed by its slides?

\include{slide1} %slide1.tex needs apackage, slide1.tex declares its own needed package
\include{slide2} %slide2.tex needs bpackage, slide2.tex declares its own needed package

And (not working code) slide1.tex is


This is of course a feature very common in languages like C, in which it included file can declare their own dependencies.

Of course it could be that the solution is to write this main.tex, but that would be terrible.

  • Are (the contents of) apackage and bpackage in conflict with each other? If they're not, why not just load them both? Aside: You've already "discovered" that \usepackage is a preamble-only instruction, right?
    – Mico
    Commented Oct 10, 2017 at 4:33
  • @Mico They are not necessarely in conflict. Yes, I know that, I am looking for a workaround to make the writing of documents (slides actually) more modular. (For example, to have multiple presentations that reuse different sets of slides, without loading unnecessary packages or maintaining main files).
    – alfC
    Commented Oct 10, 2017 at 4:36
  • You can try \input{apackage.sty} or \makeatletter\input{apackage.sty}\makeatother instead of \usepackage{apackage}. For some packages it might work, but probably for most it won't.
    – Scz
    Commented Oct 10, 2017 at 6:37

3 Answers 3


David Carlisle has already mentioned the standalone package, this is a MWE how to use it:



% https://tex.stackexchange.com/q/120060/120953
% "packages that do some of their jobs \AtBeginDocument are likely to fail" with subpreambles (egreg)
% => must be loaded in main document as well




\pgfdeclarelindenmayersystem{Koch curve}{\rule{F -> F-F++F-F}}

\tikz\shadedraw[shading=color wheel] [l-system={Koch curve, step=2pt, angle=60, axiom=F++F++F, order=4}] lindenmayer system -- cycle;




\begin{tabular}{ c S[table-format=7.6] }
Prefix & {Value} \\
\si{\mega} & 1 000 000 \\
\si{\kilo} & 1 000 \\
\si{\milli} & .001 \\
\si{\micro} & .000 001 \\

content/section-01.tex and content/section-02.tex can be compiled on there own as well as be included into main.tex.

  • Ah, subpreambles, I didn't know about it. This sound more like what I am looking for. Plus, I have standalone per slide preview.
    – alfC
    Commented Oct 10, 2017 at 7:35

In general no. (If comparing with C remember that TeX is a macro processor, so you should compare with the C pre-processor and any definitions made by two #include files in the same file). Packages can only be included in the preamble.

However for a slide, if it is a self-standing chunk of text with little dependency on text in other slides then you have the possibility of compiling the slides as separate 1-page documents and then including the generated pdf, so


the standalone package has some facilities for arranging document inclusion using this kind of technique in the background, or you can just do it directly.

  • Actually standalone is one step closer to what I want. It would be perfect if the package could read ahead the dependencies from the standalone included files.
    – alfC
    Commented Oct 10, 2017 at 6:55
  • @alfC if using a standalone-like system the included files have no dependencies as they are processed separately, but perhaps I misunderstood your comment. Commented Oct 10, 2017 at 7:49
  • You understood perfectly, the feature that @jakun mentioned solves this long standing problem.
    – alfC
    Commented Oct 10, 2017 at 9:27

From the comments it seems that what you really want to do is to be able to load particular packages only when they are needed by particular include files. As other people have said, you can't do this in the included files because \usepackage{...} needs to go into the preamble. You presumably also want this to be fairly "automatic" so that you do not have to keep on checking to see exactly which packages are needed by each of the included files.

What you can do is create your own "style file" that loads all of the required packages for you. I am thinking of a "package" myfileoptions.sty that you load with a command like


and the myfileoptions package is smart enough to know which packages to load for the input files filea.tex and fileb.tex.

Here is a possible mock-up for myfileoptions.sty :



\pgfkeys{/myfileoptions/.is family, /myfileoptions,
  filea/.code = {\global\Tikztrue},
  fileb/.code = {\global\Tcolorboxtrue},
  filec/.code = {\global\Tikztrue

\ProcessPgfOptions{/myfileoptions}% process options



All this does is load the pgfopts package to process the options given to the class file. Inside the class file there are a bunch if \newif statements for each possible loaded package and \pgfkeys is used to turn these switches on whenever a file needs said package. At the end of myfileoptions.sty all of the needed packages are loaded using the corresponding \newif.

Of course, you will still need to have \include{filea} etc at the place(s) where you want to insert filea.

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