I've got a standalone document that uses siunitx:

% subdocument.tex
\documentclass{standalone}
\usepackage{siunitx}
\begin{document}
\SI{10}{\kilo\newton}
\end{document}


I'd like to include this standalone document as a sub-document in a different document, using the standalone package. Because I don't want to manually keep track of which packages are needed for the sub-document, I want to use the subpreambles=true option of standalone:

% main.tex
\documentclass{article}
\usepackage[subpreambles=true]{standalone}

\begin{document}
\input{subdocument.tex}
\end{document}


However, if I do that, I get an error message:

! Undefined control sequence.
<argument> \l__siunitx_option_deactivate_clist


I could fix this by loading siunitx in main.tex, but I hope that there is a different way around this.

Here's a MWE:

\documentclass{article}
\begin{filecontents}{subdocument.tex}
\documentclass{standalone}
\usepackage{siunitx}
\begin{document}
\SI{10}{\kilo\newton}
\end{document}
\end{filecontents}

%\usepackage[subpreambles=false]{standalone}
%\usepackage{siunitx}

% This doesn't (loading siunitx in the sub-document), but it would be nice if it did:
\usepackage[subpreambles=true]{standalone}

\begin{document}
\input{subdocument.tex}
\end{document}

-
I'm afraid that this isn't a good way to proceed; packages that do some of their jobs \AtBeginDocument are likely to fail here. I see subpreambles=true for defining macros used in the subdocument, not for loading packages. –  egreg Jun 19 '13 at 20:17
As the documentation of standalone explains, the packages loaded in the subpreambles are stored in an auxiliary file and they are eventually loaded using \AtBeginDocument. But when \@begindocumenthook is executed, \AtBeginDocument is redefined to be \@gobble, so no code those packages want to delay at begin document sees the light. –  egreg Jun 19 '13 at 20:25
as @cgnieder figured out in chat, it works when using pure \usepackage{standalone} in combination with \unexpanded siunits. Therefore, you need to load siunitx within the main document –  Rico Jun 20 '13 at 7:14
could standalone then use the etoolbox \AtEndPreamble rather than \AtBeginDocument? –  jfbu Nov 7 '13 at 9:35
@egreg: Can we cancel the redefinition \AtBeginDocument to \@gobble? –  kiss my armpit Nov 8 '13 at 20:45

It turns out that just adding \AtBeginDocument{\renewcommand{\AtBeginDocument}[1]{}} before loading the standalone package makes the MWE compile correctly. This is definitely not the correct solution since this throws away all the things that the siunitx package thinks need to be done in \AtBeginDocument. If instead we go with an MWE of

\documentclass{article}
\usepackage{etoolbox}
\newcommand{\AtBeginSubDocument}{}
\AtBeginDocument{\renewcommand{\AtBeginDocument}[1]{\apptocmd{\AtBeginSubDocument}{#1}{}{}}}
\usepackage[subpreambles=true]{standalone}

\AtBeginDocument{\meaning\AtBeginSubDocument}
\AtBeginDocument{\AtBeginSubDocument}
\begin{document}
\input{subdocument.tex}
\end{document}


The MWE compiles and shows that about 2 pages worth of content is being checked in \AtBeginDocument. Much of this looks like checks to see if packages are loaded (i.e., @ifpackageloaded) and there may be package combinations that cause problems.

-