Is it possible to usepackage inside an input or include file?
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.
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}
\usetikzlibrary{lindenmayersystems,shadings}
\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.
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
.