I am trying to use TeX's ability to dump a "format" file along with the standalone package. There are four scenarios to consider, and I can get all but 1 working.
First - the files I'm using. There is pre.tex, which loads all my packages and will get turned into my format file. It has the following contents:
%&PDFLATEX
\makeatletter\@ifclassloaded{standalone}{}
{\documentclass[class=report]{standalone}
\usepackage{standalone}
\def\preambledocclass{}}\makeatother
\usepackage{geometry}
Next, I have main.tex, which includes one file, sub.tex:
%&pre
\makeatletter\@ifundefined{preambledocclass}{%%
\documentclass[class=report]{standalone}
\usepackage{standalone}
\input{pre}}{}\makeatother
\begin{document}
\section{main}
\input{sub}
\end{document}
Finally, I have sub.tex, which does not include any other files:
%&pre
\makeatletter\@ifundefined{preambledocclass}{%%
\documentclass[class=report]{standalone}
\usepackage{standalone}
\input{pre}}{}\makeatother
\begin{document}
\section{sub-document}
\end{document}
My goal is to be able to compile sub independently of main. Additionally, I want to create a FMT file out of pre and have both sub and main use that format file (if present).
To create the FMT file, I run pdflatex -ini pre \dump .
Notice that main and sub both input pre; However, when pre.fmt is available, pre is effectively inputted before main or sub begin. The check for preambledocclass at the beginning of sub and main detects when pre.fmt has been loaded.
There are four scenarios to consider, depending on if pre.fmt is available.
pre.fmtNOT available:pdflatex sub-- Success.pdflatex main-- Success.
pre.fmtavailablepdflatex sub-- Success.pdflatex main-- I get the following error:(./sub.tex ! LaTeX Error: Can be used only in preamble. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.6 \begin{document} ?
If I change sub so it declares a documentclass when pre.fmt is loaded, as in:
%&pre
\makeatletter\@ifundefined{preambledocclass}{%%
...}{\documentclass{standalone}}\makeatother
Then I can no longer compile sub independently (Scenario 2.1). I get a double documentclass declaration error. Adding a check to see if \documentclass equals \@twoclasseserror, as in:
%&pre
\makeatletter\@ifundefined{preambledocclass}{%%
...}{\ifx\documentclass\@twoclasseserror\else\documentclass{standalone}\fi}\makeatother
fixes sub, but breaks main (Scenario 2.2), resulting in an ugly error:
(./sub.tex
! Extra }, or forgotten \endgroup.
\sa@gobble ...reamble@\sa@filepath \endcsname {#1}
\@ifundefined {sa@written@...
l.6 \begin{document}
?
How do I successfully compile all 4 scenarios above?
BTW, I am using Version v0.4a – 2011/04/07 of standalone.

standaloneclass together with the package for both the main and the sub file. Are you sure that is what you want? If the main file is a normal document you should use thereportclass directly and only load thestandalonepackage. Also note that v1.0 is out since 2011/12/21 and that thestandalonepackage redefines\documentclass, which might be a cause for trouble here. – Martin Scharrer♦ Jan 26 '12 at 21:58geometrypackage with thestandaloneclass is a little suspicious. That normally doesn't make much sense, because the class is mostly about cropping the content making a predefined page geometry unnecessary. – Martin Scharrer♦ Jan 26 '12 at 22:01