I am using Create a \savebox in a Group and have it available outside of group to cache some math content. However, I realized that there may be a case where the same math content needs to be cached for different math styles.
I attempted to make use of the scalerel package mentioned at Proper-use-of-mathchoice and include that in the style. But, I am not able to get the functionality of \SavedStyle to be applied to the math content. The MWE below produces:
The desired output has a smaller \sqrt in the denominator:
Code:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{scalerel}
\NewDocumentCommand{\CachedMathContent}{s O{} m}{%
% #1 = not used here
% #2 = String to use to store cached name.
% Declared as optional (for compatibility with another related macro), but
% in this example it is really mandatory.
% ---------
% Code to check that #2 was provided has been omitted here.
\ThisStyle{% scalerel pacakge
\begingroup
\edef\CachedMathBoxName{MathBox #2 \string\SavedStyle}%
\ifcsdef{\CachedMathBoxName}{%
%% Cached content already exists
}{%
%% https://tex.stackexchange.com/questions/283373/create-a-savebox-in-a-group-and-have-it-available-outside-of-group
\global\expandafter\newsavebox\csname\CachedMathBoxName\endcsname%
\global\expandafter\setbox\csname\CachedMathBoxName\endcsname%
\hbox{$\SavedStyle#3$}%
}%
\expandafter\usebox\csname\CachedMathBoxName\endcsname%
\endgroup
}%
}%
\begin{document}
Should be cramped: $\frac{1}{\CachedMathContent[sqrt phi]{\sqrt{\phi}}}$
\medskip
Should NOT be cramped: $\CachedMathContent[sqrt phi]{\sqrt{\phi}}$
\end{document}



scalerelpackage handled math styles and it does for most cases, but I can't make it work as in the MWE for some reason. – Peter Grill Jan 08 '16 at 11:56