I need to scale Tikz pictures automatically, and I do this using the following custom environment:
% Automagically scale a Tikz picture, so it has the desired (given) width.
% Does NOT scale line width/text width! Needs the package "environ"!
%
% Usage:
%
% \begin{myscaletikzwidth}{\textwidth}
% \begin{tikzpicture}[scale=\tikzscale]
% ..
% \end{tikzpicture}
% \end{myscaletikzwidth}
%
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{myscaletikzwidth}[1]{%
\def\tikz@width{#1}%
\def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
\BODY
\end{lrbox}%
\pgfmathparse{#1/\wd\measure@tikzpicture}%
\edef\tikzscale{\pgfmathresult}%
\BODY
}
\makeatother
(I got that code from another topic on Stackexchange.)
This code works perfectly fine, however it doesn't work anymore, when I turn on externalization (graphics will then be created in their original size).
How can I make auto-scaling work in conjunction with externalization?
I am using the modern externalization approach that has been introduced with newer Tikz versions ("\tikzexternalize").
Maybe some hack with writing "\def\tikzscale{MyDeterminedScaleFactor}" to an external file and inputting that file afterwards?
Could you please help me with that?