I am trying to use the subpreambles option with the standalone package. My main file is:



Before Input: MyToken = \the\MyToken  \par
\input{MySubfile.tex} \par
After Input: MyToken = \the\MyToken

and the MySubfile.tex content is:


\ifdefined\MyToken% prevent redfintion.


In sub file: MyToken = \the\MyToken

The output from this is:

Before Input: MyToken = Subfile
In sub file: MyToken = Subfile
After Input: MyToken = Subfile

but I what I want is this:

Before Input: MyToken = MainFile
In sub file: MyToken = Subfile
After Input: MyToken = MainFile

I tried using \begingroup \endgroup around the input and that did not work. I don't want to use different tokens in the standalone and main files, as what is currently the main file, will eventually also be a subfile. I am not stuck using \newtoks, any method that I can use to achieve this is fine.

I am also ok with last line printing

Input: MyToken = Subfile

Is this possible?

  • Why are you not just checking for \ifdefined\MyToken instead? The \TokensDeclared isn't really required. – Martin Scharrer Apr 21 '11 at 8:33

The subpreamble option of the standalone package (you set it on the class for some reason, which works as well because the classes are ignoring unknown options and passing all options to all packages) copies the sub-preambles (i.e. the preambles of the sub-files) to the preamble of the main file. The preambles are written into an auxiliary file and read in again in future runs in the preamble, not when the sub-files are read, of course.

Therefore \MyToken={Subfile} is executed in the main preamble, not in the \input. You will have to move it after the \begin{document} in the sub-file to make it work as you want.

  • I thought of doing that, but that won't quite work for me as I need access to those token at \begin{document}, and would really prefer to NOT have to duplicate the same information before and after \begin{document}. Is there possibly another way I can do achieve this? – Peter Grill Apr 21 '11 at 8:45
  • @Peter: You could use the filehook package and place them into a file hook for that file: \AtBeginOfInputFile{MySubfile}{...}. This would move that code from the main preamble to the \input command. However, you need to make sure that the code is executed right away when the subfile is compiled alone. There is the \ifstandalone switch, but it might not work if the main file also uses the standalone class. – Martin Scharrer Apr 21 '11 at 9:00
  • I don't think that will work for me. It seems that that will just move the main preamble settings to the subfile. The subfile has its own settings and I need those in the subfile. It guess what I am looking for is something that just ignores the \documentclass and \begin{document}, and \end{document} in all \input files. So, perhaps I can use the filehook package to do that? – Peter Grill Apr 21 '11 at 9:13
  • @Peter: The standalone class ignores \documentclass and \begin{document} and everything between them. I think the last part is was causes problems for you. The filehook package won't help you here, only in combination with standalone. – Martin Scharrer Apr 21 '11 at 9:18
  • I was thinking of switching to not use standalone, and use filehook to set \usepackage, \begin{document}, and \end{document} to gobble, with the \AtBeginOfEveryFile and switch them back to normal via \AtEndOfEveryFile. Not exactly sure how to do all that, but am willing to give it a try unless there is something horribly wrong with that logic? The documentation for filehook seems to indicate that it will do proper nesting, so that once I return back to the main document, the \end{document} should work fine. Any thoughts on this approach? – Peter Grill Apr 21 '11 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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