Using \savebox within a tikzpicture environment seems wipe out the content of the \savebox.
In the MWE below, the following macros define, measure and display the size of a \savebox:
\newcommand*{\DefineBoxAndItSize}{%
\savebox\tempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
\setlength\SizeOfBox{\dimexpr\ht\tempboxA+\dp\tempboxA\relax}%
}%
\newcommand*{\ShowBoxAndSize}{%
Box \fbox{\usebox{\tempboxA}} is \printlength{\SizeOfBox}%
}
As long as \DefineBoxAndItSize is invoked outside of a tikzpicture, using \ShowBoxAndSize both outside (first line of output) and inside a tikzpicture environment (second line of output) works fine.
However, the third line invokes \DefineBoxAndItSize inside a tikzpicture before \ShowBoxAndSize, which ends up making the box empty.
Question:
How can I define a \savebox within a tikzpicture environment?
Code:
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{printlen}
\newsavebox{\tempboxA}
\newlength{\SizeOfBox}
\fboxsep=0pt
\newcommand{\DefineBoxAndItSize}{%
\savebox\tempboxA{Hg}%
%% https://tex.stackexchange.com/a/11945/4301
\setlength\SizeOfBox{\dimexpr\ht\tempboxA+\dp\tempboxA\relax}%
}%
\newcommand{\ShowBoxAndSize}{%
Box \fbox{\usebox{\tempboxA}} is \printlength{\SizeOfBox}%
}
\begin{document}
\DefineBoxAndItSize%
\noindent
Outside TikZ: \ShowBoxAndSize.
\medskip\noindent
\begin{tikzpicture}[inner sep=0pt, anchor=east]
\node at (0,0) {Use inside TikZ: \ShowBoxAndSize.};
\end{tikzpicture}%
\medskip\noindent
\begin{tikzpicture}[inner sep=0pt, anchor=east]
\DefineBoxAndItSize% <--- What is wrong with this?
\node at (0,-1) {Define inside TikZ: \ShowBoxAndSize.};
\end{tikzpicture}%
\end{document}


\begin{pgfinterruptpicture} \DefineBoxAndItSize \end{pgfinterruptpicture}works. You want to post an answer? Not sure I would imemdiately have seen this as a related issue had I seen the referenced question. – Peter Grill Nov 13 '18 at 22:09\begin{pgfinterruptpicture} ... \end{pgfinterruptpicture}to their macro (as well). I agree that your question is not a precise duplicate. I can add an answer, but I also think there would be nothing wrong if you answer your own question. Please let me know what you like better. – Nov 13 '18 at 22:16lrboxwas needed there. Doesn't seem to be for this case. I can certainly post an answer, but don't feel right as I didn't solve it. – Peter Grill Nov 13 '18 at 22:28