This was fixed in version from 2017/06/15 (available on CTAN).
This is a followup question of How to transfer math style in \hbox_set?.
While the code I posted there as an answer using LuaTeX’s \mathstyle primitive it doesn’t work inside of the (de)nominator of a \frac. If a \frac happens to appear in \textstyle the (de)nominator actually is set in \scriptstyle (as far as I can tell), but \mathstyle stell equals 2.
% !TeX program = luatex
\documentclass{article}
\usepackage{parskip,xparse,xcolor}
\usepackage{amsmath,lualatex-math}
\ExplSyntaxOn\makeatletter
\cs_new:Npn \tobi_save_math_style: {
\int_case:nn { \mathstyle } {
{ \displaystyle } { \tl_set:Nn \l_tobi_saved_math_style_tl { \displaystyle } }
{ \textstyle } { \tl_set:Nn \l_tobi_saved_math_style_tl { \textstyle } }
{ \scriptstyle } { \tl_set:Nn \l_tobi_saved_math_style_tl { \scriptstyle } }
{ \scriptscriptstyle } { \tl_set:Nn \l_tobi_saved_math_style_tl { \scriptscriptstyle } }
}
}
\NewDocumentCommand { \boxtest }{ m }{
\mode_if_math:TF {
% aktuellen Mathemodus ermitteln und für später sichen
\tobi_save_math_style:
% Box mit entsprechender Formel speichern
\hbox_gset:Nn \l_tmpa_box {
\begingroup
\(
\m@th
\tl_use:N \l_tobi_saved_math_style_tl
#1
\)
\endgroup
}
} {
\hbox_set:Nn \l_tmpa_box { #1 }
}
\fbox { \box_use:N \l_tmpa_box }
[\tl_to_str:n { \mathstyle } = \mathstyle; saved style = \tl_to_str:N \l_tobi_saved_math_style_tl] % for testing!
}
\cs_new:Npn \tobi_genfrac:nnnn #1#2#3#4 {
% \colorlet { l_tobi_current_color_before_frac } { . }
% \begingroup
% \color { #2 }
\genfrac { } { } { } { #1 } {
% \color { l_tobi_current_color_before_frac }
#3
} {
% \color { l_tobi_current_color_before_frac }
#4
}
% \endgroup
}
\RenewDocumentCommand { \frac } { O{.} m m } {
\tobi_genfrac:nnnn { } { #1 } { #2 } { #3 }
}
\makeatother\ExplSyntaxOff
\begin{document}
OK: $a^2 = a^{\boxtest{2}}$
OK: $\displaystyle \frac{1}{2} = \frac{\boxtest{1}}{2}$
OK: $\frac{1}{2} = \boxtest{\frac{1}{2}}$
OK: $\displaystyle \frac{1}{2} = \boxtest{\frac{1}{2}}$
wrong: $\frac{1}{2} \neq \frac{\boxtest{1}}{2}$
wrong: $\genfrac{}{}{}{}{1}{2} \neq \genfrac{}{}{}{}{\boxtest{1}}{2}$
\end{document}
So … is it possible to capture the correct style in this case too?
Update: I added an example using \genfrac, which is used in my real document do get a coloured fraction line.
