Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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. :-(

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

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;)).

share|improve this answer
    
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 Jul 31 '11 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 Jul 31 '11 at 21:09
    
where shoud I use \usepackage{pkgindoc}? in main.tex or subfile.tex (see my question). –  xport Jul 31 '11 at 21:19
    
In main.tex (more precisely, before \begin{document}) - it just enables you to use \usepackage anywhere. –  mbork Jul 31 '11 at 21:24
1  
There is no need to create .ins files. Just run tex docstrip. –  Andrey Vihrov Aug 1 '11 at 6:26
show 1 more comment

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.

share|improve this answer
    
+1 Does the main file do a test to avoid multiple inclusion of the same package? –  xport Jul 31 '11 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) –  Martin Scharrer Jul 31 '11 at 18:45
add comment

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
share|improve this answer
    
+1 Will it cause a problem if I include many different subfiles and each subfile inputs PSTricks related packages? –  xport Jul 31 '11 at 18:11
1  
no, every PSTricks package does a test if it is already loaded –  Herbert Jul 31 '11 at 18:20
    
+1 thanks, it is a good news! –  xport Jul 31 '11 at 18:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.