我在用在组中创建一个 \savebox,并使其在组外可用缓存一些数学内容。但是,我意识到可能存在需要为不同的数学风格缓存相同数学内容的情况。
scalerel
我尝试使用正确使用数学选择并将其包含在样式中。但是,我无法将 的功能\SavedStyle
应用于数学内容。下面的 MWE 产生:
\sqrt
期望输出的分母较小:
代码:
\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}
答案1
\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}
代码的目的是根据提供的参数和当前样式保存一个框,但是
\edef\CachedMathBoxName{MathBox #2 \string\SavedStyle}%
使用并且它不使用#2
文字字符串的定义(它根本不需要定义它)。\SavedStyle
\SavedStyle
您需要将该宏完全扩展为类似内容\textstyle
,然后\string
应用于那因此\edef
和\expandafter
。