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? – user4686 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.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7229169011116028, "perplexity": 2876.002097929}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-43/segments/1570986664662.15/warc/CC-MAIN-20191016041344-20191016064844-00302.warc.gz"}