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.

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.

  1. pre.fmt NOT available:
    1. pdflatex sub -- Success.
    2. pdflatex main -- Success.
  2. pre.fmt available

    1. pdflatex sub -- Success.
    2. 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.

share|improve this question
    
You are using the standalone class 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 the report class directly and only load the standalone package. Also note that v1.0 is out since 2011/12/21 and that the standalone package redefines \documentclass, which might be a cause for trouble here. –  Martin Scharrer Jan 26 '12 at 21:58
    
Also using the geometry package with the standalone class 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
add comment

1 Answer

up vote 3 down vote accepted

The problem here is that you need a \documentclass in your sub file, so that its preamble is skipped. The skipping code is part of the redefined version of \documentclass. If that macro is never executed the \begin{document} is never redefined and causes the usually error. You need to add some \documentclass in all cases when the sub file is compiled as part of the main document. Because you are using the standalone class for both the main and sub file you can't use the normal if-switches and macros provided by standalone because they are true in both cases.

The idea would be to check if \documentclass is equal to \@twoclasseserror, but you must not enclose the \documentclass inside any \if... \fi clause, because the \fi will be skipped then with the rest of the preamble. You need to change your code to close the if-clause before \documentclass is processed.

Please note that the \section command will cause trouble when used with the crop option new in v1.0 and selected as default. You should therefore set the preview option explicitly (it is the default for version v0.x).

Here the three files with the required changes:

pre.tex

%&pdflatex
\makeatletter\@ifclassloaded{standalone}{}
  {\documentclass[class=report,preview]{standalone}
    \usepackage{standalone}
    \def\preambledocclass{}}\makeatother
\usepackage{geometry}

main.tex

\documentclass[preview]{standalone}
\usepackage{standalone}
\def\preambledocclass{}
\begin{document}
\input{sub}
\end{document}

sub.tex

%&pre
\makeatletter
\@ifundefined{preambledocclass}{%%
  \documentclass[class=report,preview]{standalone}
  \usepackage{standalone}
  \input{pre}
}{%
 \ifx\documentclass\@twoclasseserror
    \expandafter\@gobble
 \else
    \expandafter\@firstofone
 \fi
 {\documentclass{standalone}}%
}
\makeatother
\begin{document}
\section{sub-document}
\end{document}

Tested with both v0.4a and v1.0 of the standalone bundle.

Note that you must write %&pdflatex in pre.tex, not %&PDFLATEX, otherwise you will get an error under case-sensitive operation systems like Linux.

share|improve this answer
    
Thanks so much! That worked. Would it also work to assign \documentclass{standalone} to a token register in the \else case above, and then expand the token register after the conditional? –  Justin Bailey Jan 26 '12 at 23:49
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.