How are subpreambles handled by the standalone package?

I wrote two simple files integrated using the standalone package:

main.tex:

\documentclass{article}
\usepackage[subpreambles]{standalone}

\begin{document}

\input{myfile}

\end{document}


myfile.tex:

\documentclass[class=article, crop=false]{standalone}
\usepackage{lipsum}
\usepackage{acronym}

\begin{document}

\acrodef{FB}{Foo Bar}
First call: \ac{FB} \par Later calls: \ac{FB}

\lipsum[1]

\end{document}


While the subfile compiles as intended, the main file compiles with error:

Seems like while the \lipsum and the \ac commands are both known to the main file, the \acrodef command is not so as suggested by the errors I get. Even if I move the \acrodef command to the preamble of the subfile, nothing changes.

The only way I could solve this so far was to add the acronym package also in the preamble of the main file while based on the documentation of the standalone package, this is what the subpreambles tag is expected to do and has already done with the lipsum package in this example.

So what am I missing or misunderstanding about the standalone package and how it handles the subpreambles?

Update

With using the command \newacro instead of \acrodef, the issue with the command \ac gets solved. So the first call and the other calls to \ac successfully resolves to correct outputs. But still there is an additional line showing up in the output when compiling main.tex (the first line):

The problem is that the .aux file where acronym puts the seemingly undefined stuff, is input before before the .sta file where standalone puts the subpreambles. The input of the .sta file happens when the begindocument hook is executed, which happens after the .aux file is being read.

However, there is a hook begindocument/before which happens before the .aux file is read.

In your example we can add the crucial code from standalone to this hook rather safely as your subfile only loads packages. Packages never get loaded twice so this is not a problem but custom definitions in a subpreamble may very well be a peoblem… use the following with caution.

Also: you should report this to the package author.

\documentclass{article}
\usepackage[subpreambles]{standalone}

\makeatletter
\let\subpreamble\@gobble
\let\endsubpreamble\relax
\let\standalonepreambles\relax
\let\endstandalonepreambles\relax
\ifsa@sortsubpreambles
\let\sa@orig@usepackage\usepackage
\let\usepackage\sa@usepackagewithoutoptions
\fi
\InputIfFileExists{\jobname.sta}{}{}%
}
\makeatother

\begin{document}

\input{myfile}

\end{document}


• Thanks! And to which author should it be reported? standalone or acronym? From your answer I guess the standalone package should be responsible for this, right? – mhr Jan 10 at 9:32
• @mhr yes, this needs to be changed in the standalone package – cgnieder Jan 10 at 9:47