10

It seems strange but I really need this. Is there a built-in macro to load additional packages specified in the document environment?

% main.tex
\documentclass{article}
\begin{document}
\input{subfile}
\end{document}
% subfile.tex
\LoadPackage{pstricks}
\LoadPackage{multido}
\pspicture(1,1)
\psframe(1,1)
\endpspicture

I accepted mbork's answer because it really allow us to load some packages after \begin{document}. I have tested the following and it is successfully compiled with a PDF output.

% main.tex
\documentclass{article}
\usepackage{pkgindoc}
\begin{document}
\input{subfile}
\end{document}
% subfile.tex
\usepackage{xcolor}
I can find a tool to convert PDF to EPS in my neither bathroom nor kitchen.

Unfortunately, using pstricks, geometry, hyperref (and among others) do not work. :-(

1

3 Answers 3

5

Now apparently not many people know the following trick.

Make a pkgindoc.ins file with the following contents:

\input docstrip
\generate{\file{pkgindoc.sty}{\from{ltclass.dtx}{afterpreamble}}}
\endbatchfile

in some temporary directory/folder. Copy the file ltclass.dtx to the same directory and run latex pkgindoc.ins. You will obtain a file pkgindoc.sty; copy it wherever needed. When you \usepackage{pkgindoc}, the commands declared in ltclass.dtx as \@onlypreamble (like \usepackage) will be available after \begin{document}.

Note: this seemingly won't help if some package contains other commands declared as \@onlypreamble, e.g. by some other package; but this I know only from experiments, I haven't studied pkgindoc.sty (yet;)).

6
  • And what is the problem? 1. You create the file pkgindoc.ins. 2. You copy ltclass.dtx to the same folder. 3. You run LaTeX on pkgindoc.ins. 4. You copy pkgindoc.sty to the folder with your document. 5. You \usepackage{pkgindoc} and \usepackage works after \begin{document} (but other things might break). That's it. Funny thing is, that this is in LaTeX2e sources, but apparently is not used anywhere. And there's no description on CTAN, either!
    – mbork
    Commented Jul 31, 2011 at 20:50
  • OK, so I looked at pkgindoc.sty and I have to say this is one of the dirtiest tricks I've seen; not really diffcult to understand, but just ugly. It is a hack which removes a batch of macros (connected with package writing) between two specified ones from the list to "forbid" after the preamble. I'm not really sure what this served originally, I'll ask that in a minute;).
    – mbork
    Commented Jul 31, 2011 at 21:09
  • where shoud I use \usepackage{pkgindoc}? in main.tex or subfile.tex (see my question). Commented Jul 31, 2011 at 21:19
  • In main.tex (more precisely, before \begin{document}) - it just enables you to use \usepackage anywhere.
    – mbork
    Commented Jul 31, 2011 at 21:24
  • 1
    There is no need to create .ins files. Just run tex docstrip. Commented Aug 1, 2011 at 6:26
5

The subpreambles option of the standalone package will collect the preambles of included sub-files and read them in the main preamble at the next compiler run. However for this you need a full preamble and document environment in the sub-files.

2
  • +1 Does the main file do a test to avoid multiple inclusion of the same package? Commented Jul 31, 2011 at 18:36
  • @xport: Yes, this is supported. However, multiple \usepackage{xxx} are fine anyway and will only result in the package being loaded once. A more important features is the sorting of package options (enabled using the sort option) to avoid package option clashes if two sub-files load the same package with different options. (Of course this also fails if the two options don't mix, but this can't be avoided in an automatic way) Commented Jul 31, 2011 at 18:45
3

packages can only be loaded in the preamble part of the document. However, with PSTricks it is a special situation, you can do:

% subfile.tex
\input{pstricks}
\input{multido}
\pspicture(1,1)
\psframe(1,1)
\endpspicture
2
  • +1 Will it cause a problem if I include many different subfiles and each subfile inputs PSTricks related packages? Commented Jul 31, 2011 at 18:11
  • 1
    no, every PSTricks package does a test if it is already loaded
    – user2478
    Commented Jul 31, 2011 at 18:20

You must log in to answer this question.

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