2

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:

enter image description here

The desired output has a smaller \sqrt in the denominator:

enter image description here

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}

Peter Grill
  • 223,288
  • 2
    the whole point of boxes is that typesetting is frozen, if you need different settings why not just use a command (macro) ? – David Carlisle Jan 08 '16 at 11:47
  • @DavidCarlisle: But I only occasionally use different styles. In most cases there will only be one style. This was just to handle that case. But, yes, I have been rethinking things -- original motivation for doing this was for efficiency in re-typesetting (and other related functions). – Peter Grill Jan 08 '16 at 11:50
  • @DavidCarlisle: Thought the scalerel package 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
  • oh your example code is wrong! hang on... – David Carlisle Jan 08 '16 at 12:01

1 Answers1

5

enter image description here

\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\x{\SavedStyle}%
\typeout{\meaning\x}%
        \edef\CachedMathBoxName{MathBox #2 \expandafter\string\x}%
        \ifcsdef{\CachedMathBoxName}{%
            %% Cached content already exists
        }{%
            %% http://tex.stackexchange.com/questions/283373/create-a-savebox-in-a-group-and-have-it-available-outside-of-group
\typeout{\meaning\CachedMathBoxName}%
            \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}}}$
$\frac{1}{\sqrt{\phi}}$

\medskip
Should NOT be cramped:
$\CachedMathContent[sqrt phi]{\sqrt{\phi}}$ 
$\sqrt{\phi}$ 

\end{document}

The intention of the code was to save a box based on the supplied argument and the current style but

    \edef\CachedMathBoxName{MathBox #2 \string\SavedStyle}%

uses #2 and the literal string \SavedStyle it does not use the definition of \SavedStyle (it would not need it to be defined at all).

You need to fully expand that macro to something like \textstyle and then apply \string to that hence the \edef and \expandafter.

David Carlisle
  • 757,742
  • I have left it here but the \global in \global\expandafter\newsavebox can not possibly do anything useful. (it just makes the definition of \reserved@a in \@ifdefinable global) – David Carlisle Jan 08 '16 at 12:14