I am trying to build a version of beamer's alt that occupies the space of its larger argument. I do this with some saveboxes and measuring. Because I also want to use it in math, I need to save the current math style. Apparently, scalerel's \ThisStyle doesn't interact perfectly with beamer overlays, so I'm trying to get along without. However, I have difficulties determining the current math style. Note that the usual mathpalette-way causes problems with the overlay specification.
I came up with the following, which neither works nor without the aftergroups.
\documentclass{beamer}
\usepackage{calc}
\show\mathchoice
\newcommand\ThisStyle[1]{
\mathchoice
{\aftergroup\let\aftergroup\SavedStyle\aftergroup\displaystyle}
{\aftergroup\let\aftergroup\SavedStyle\aftergroup\textstyle}
{\aftergroup\let\aftergroup\SavedStyle\aftergroup\scriptstyle}
{\aftergroup\let\aftergroup\SavedStyle\aftergroup\scriptscripttextstyle}
\show\SavedStyle
#1
}
\newsavebox\PhAltA
\newsavebox\PhAltB
\newlength\PhAltWidth
\newlength\PhAltHeight
\newlength\PhAltDepth
\newcommand<>{\phalt}[3][c]{{%
\ifmmode%
\def\dollar{$}%
\else%
\def\dollar{}%
\let\ThisStyle\relax%
\let\SavedStyle\relax
\fi%
\ThisStyle{
\savebox\PhAltB{\dollar\SavedStyle#3\dollar}%
\savebox\PhAltA{\dollar\SavedStyle#2\dollar}%
\setlength\PhAltWidth{\maxof{\wd\PhAltA}{\wd\PhAltB}}%
\setlength\PhAltHeight{\maxof{\ht\PhAltA}{\ht\PhAltB}}%
\setlength\PhAltDepth{\maxof{\dp\PhAltA}{\dp\PhAltB}}%
\alt#4{\raisebox{0pt}[\PhAltHeight][\PhAltDepth]{\makebox[\PhAltWidth][#1]{\usebox\PhAltA}}}%
{\raisebox{0pt}[\PhAltHeight][\PhAltDepth]{\makebox[\PhAltWidth][#1]{\usebox\PhAltB}}}%
}%
}}
\begin{document}
\begin{frame}
\onslide<+->
X\phalt<+->{A}{BBB}X
$X\phalt<+->{A}{BBB}X$
$A_{X\phalt<+->{A}{BBB}X}$
\onslide<+->
\end{frame}
\end{document}
If you use the scalerel definition, you'll see that spurious overlays are inserted.

\mathchoice {\aftergroup\let\aftergroup\SavedStyle\aftergroup\displaystyle} {\aftergroup\let\aftergroup\SavedStyle\aftergroup\textstyle} {\aftergroup\let\aftergroup\SavedStyle\aftergroup\scriptstyle} {\aftergroup\let\aftergroup\SavedStyle\aftergroup\scriptscripttextstyle}can not do anything useful as all four branches are always fully typeset so you get aftergroup tokens from them all, one of the four boxes is chosen later but long afterthe aftergroup tokens are seen – David Carlisle Nov 29 '23 at 14:33\mathchoice{\global\let...}doesn't work. – Bubaya Nov 29 '23 at 14:39\mathchoiceinside\alt? – Bubaya Nov 30 '23 at 07:58\phantomfor example is defined which boxes its argument then makes an empty box of same size, but does that four times – David Carlisle Nov 30 '23 at 09:19