# Is it possible to usepackage inside an input or include file?

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.

\documentclass{beamer}
\usepakcage{apackage}
\usepackage{bpackage}
\title{Title}
\author{hola}
\begin{document}
\include{slide1} %slide1.tex needs apackage
\include{slide2} %slide2.tex needs bpackage
\end{document}


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?

\documentclass{beamer}
\title{Title}
\author{hola}
\begin{document}
\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
\end{document}


And (not working code) slide1.tex is

"\usepackage{apackage}"
\begin{frame}
\commandfromapackage{...}
\end{frame}


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.

\documentclass{beamer}
\input{slide1_packages}
\input{slide2_packages}
\title{Title}
\author{hola}
\begin{document}
\include{slide1}
\include{slide2}
\end{document}

• 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 Oct 10 '17 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 Oct 10 '17 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 Oct 10 '17 at 6:37

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

main.tex:

\documentclass{article}
\usepackage[subpreambles]{standalone}

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

\begin{document}
\input{content/section-01}
\input{content/section-02}
\end{document}


content/section-01.tex:

\documentclass[crop=false]{standalone}

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

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


content/section-02.tex:

\documentclass[crop=false]{standalone}

\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}
\section{siunitx}
\begin{center}
\begin{tabular}{ c S[table-format=7.6] }
\toprule
Prefix & {Value} \\
\midrule
\si{\mega} & 1 000 000 \\
\si{\kilo} & 1 000 \\
\si{\milli} & .001 \\
\si{\micro} & .000 001 \\
\bottomrule
\end{tabular}
\end{center}
\end{document}


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 Oct 10 '17 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

\begin{slide}
\includegraphics{slide1.pdf}
\end{slide}


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 Oct 10 '17 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. – David Carlisle Oct 10 '17 at 7:49
• You understood perfectly, the feature that @jakun mentioned solves this long standing problem. – alfC Oct 10 '17 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

\usepackage[filea,filec]{myfileoptions}


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 :

\RequirePackage{pgfopts}

\newif\ifTikz\Tikzfalse
\newif\ifTcolorbox\Tcolorboxfalse

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

\ProcessPgfOptions{/myfileoptions}% process options

\ifTikz
\RequirePackage{tikz}
\fi

\ifTcolorbox
\RequirePackage{tcolorbox}
\fi


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.