Rather than splitting the preambles of your files out manually, you might prefer to use standalone
. Basically, each of the files you want to \input
gets set up with
\documentclass{standalone}
<packages,libraries etc.>
\begin{document}
<content>
\end{document}
<content>
need not be an image. It could be anything.
All you then need to do in your main file is load the standalone
package with the options you prefer. standalone
can be configured to extract the preambles of all the files you \input
, assemble a list of packages, tikz
/pgf
libraries etc., sort the list, remove duplicates and combine any used options. It then passes the relevant options to the packages and libraries, which are loaded at the end of the preamble on your next run.
During the initial compilation, you won't get any output from the \input
files. Instead, their contents will be skipped and their preambles assembled for use on the next run. This is to ensure an attempt to typeset their content is only made when the necessary packages and libraries have been loaded. On the first run, this may well not be true because the main file need not include everything required.
Here's a simple example,
\begin{filecontents}{\jobname-1.tex}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{ decorations.pathmorphing, decorations.pathreplacing, decorations.shapes}
\usetikzlibrary{calligraphy}
\begin{document}
\edef\ylow{-1.0}
\edef\xlow{-1.0}
\edef\yhi{1.0}
\edef\xhi{1.0}
\begin{tikzpicture}[scale=4.0]
\draw[decorate, decoration={calligraphic brace,amplitude=10pt, raise=13pt}] (\xlow, \ylow) -- (\xhi, \yhi);
\node [black, above left=14pt] at (0, 0) {$\theta$};
\end{tikzpicture}
\end{document}
\end{filecontents}
\begin{filecontents}{\jobname-2.tex}
\documentclass{standalone}
\usepackage{tikz}
\usepackage{adforn}
\begin{document}
\begin{tikzpicture}
\node [font=\Huge, text=blue, draw=blue!50!green] {\adforn{60}};
\end{tikzpicture}
\end{document}
\end{filecontents}
\documentclass{article}
\usepackage[subpreambles,sort,]{standalone}
\begin{document}
A comment about the following thing.
\input{\jobname-1}
Another comment.
\input{\jobname-2}
A concluding thought.
\end{document}
On the first run, only the text of the main file is typeset. Following a second run, the digested preambles from the \input
files is loaded and their contents included.
Here's the file containing the subpreambles, \jobname.sta
:
\standalonepreambles
\subpreamble{prawf-1.tex}
\usepackage {tikz}\usetikzlibrary { decorations.pathmorphing, decorations.pathreplacing, decorations.shapes}\usetikzlibrary {calligraphy}
\endsubpreamble
\subpreamble{prawf-2.tex}
\usepackage {tikz}\usepackage {adforn}
\endsubpreamble
\endstandalonepreambles
If you'd like standalone
to write the digested subpreambles to file, too, add print
when loading the package in your main file. For the example above, \jobname.stp
contains
% Packages required by sub-files:
\usepackage{tikz}
\usepackage{adforn}
% TikZ libraries required by sub-files:
\usetikzlibrary{ decorations.pathmorphing}
\usetikzlibrary{ decorations.pathreplacing}
\usetikzlibrary{ decorations.shapes}
\usetikzlibrary{calligraphy}
% Preamble from file 'prawf-1.tex'
% Preamble from file 'prawf-2.tex'
Note that it's generally better not to include spurious spaces. In this case, it doesn't matter, but habitually including unnecessary spaces makes it easy to add them when it does. (The exception is use of the expl3
programming layer, where spaces are always discarded and you can include as many as you wish. But this design is partly motivated by the problems caused by inadvertent additions of spaces in non-expl3
code!)