[Tex/LaTex] Standalone package, preserve preamble

macrosstandalone

Is it in a way possible to preserve parts of a preamble of a standalone document?
For example when I have a savebox or a macro inside it and don't want to repeat it in the preamble of my main document. Here a mini example mypicture.tex:

\documentclass[tikz,border=5pt]{standalone}
\newsavebox\mybox
\savebox\mybox{
  \begin{tikzpicture}
    \draw[->] (0,0) -- (1,1);
  \end{tikzpicture}
}
\begin{document}
  \begin{tikzpicture}
    \node[draw] (a) at (0,0) {A};
    \node[draw] (b) at (0,2) {\usebox\mybox};
    \draw[->] (a) -- (b);
  \end{tikzpicture}
\end{document

So when I include this file inside my main document I don't want to reassign my savebox but instead reuse the one from the standalone file.

\documentclass{report}
\usepackage{standalone}
% No redeclaration of the savebox here
\begin{document}
  \input{mypicture.tex}
\end{document}

I didn't found any mechanism in the standalone manual but perhaps there is an easy solution to this problem.

Best Answer

The standalone package has a subpreambles option that preserves the preambles. Your MWE seems to have a couple of small issues (it is missing a } in \end{document} and you never load the tikz package. Fixing those two issues and adding the package option

\documentclass{report}
\usepackage[subpreambles=true]{standalone}
% No redeclaration of the savebox here
\begin{document}
  \input{mypicture.tex}
\end{document}

and

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\newsavebox\mybox
\savebox\mybox{
  \begin{tikzpicture}
    \draw[->] (0,0) -- (1,1);
  \end{tikzpicture}
}
\begin{document}
  \begin{tikzpicture}
    \node[draw] (a) at (0,0) {A};
    \node[draw] (b) at (0,2) {\usebox\mybox};
    \draw[->] (a) -- (b);
  \end{tikzpicture}
\end{document}
Related Question