0

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.

5
  • 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} 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 Commented Nov 29, 2023 at 14:33
  • @DavidCarlisle Oh. I wasn't aware of this. This also explains why \mathchoice{\global\let...} doesn't work.
    – Bubaya
    Commented Nov 29, 2023 at 14:39
  • tex.stackexchange.com/a/110637/1090 Commented Nov 29, 2023 at 14:47
  • @DavidCarlisle Which, of course, brings me to the question what the best way to achieve the desired outcome would be. Generate 8 saveboxes, and use them in a \mathchoice inside \alt?
    – Bubaya
    Commented Nov 30, 2023 at 7:58
  • you probably only need one box but do the whole thing inside a mathchoice, look how \phantom for example is defined which boxes its argument then makes an empty box of same size, but does that four times Commented Nov 30, 2023 at 9:19

1 Answer 1

1

Does this what you want?

\documentclass{beamer}
\usepackage{calc}

\newsavebox\PhAltA
\newsavebox\PhAltB

\newcommand\phaltbox[5]{%
  % #1 - position ( l / r / c / s )
  % #2 - material to measure and typeset
  % #3 - material to measure but not to typeset
  % #4 - if in mathmode: math-style-command, if not in mathmode: empty
  % #5 - if in mathmode: $, if not in mathmode: empty
  \savebox\PhAltA{#5#4#2#5}%
  \savebox\PhAltB{#5#4#3#5}%
  \raisebox{0pt}[{\maxof{\ht\PhAltA}{\ht\PhAltB}}]%
                [{\maxof{\dp\PhAltA}{\dp\PhAltB}}]%
                {%
                  \makebox[{\maxof{\wd\PhAltA}{\wd\PhAltB}}]%
                          [{#1}]%
                          {#5#4#2#5}% <- doing \usebox here would outmaneuver \makebox's [s]-option.
                }%
}%

\newcommand\phaltboxmode[3]{%
  \ifmmode
    \mathpalette{\phaltbox{#1}{#2}{#3}}{$}%
  \else
    \phaltbox{#1}{#2}{#3}{}{}%
  \fi
}%

\newcommand<>{\phalt}[3][c]{%
  \alt#4{%
    \phaltboxmode{#1}{#2}{#3}%
  }{%
    \phaltboxmode{#1}{#3}{#2}%
  }%
}%

\begin{document}
\begin{frame}
  \onslide<+->
  X\phalt<+->[s]{A A A}{\tiny{B} \tiny{B} \tiny{B}}X
  $X\phalt<+->{AAA}{BBB}X$
  $X_{X\phalt<+->{AAA}{BBB}X}$
  \onslide<+->
\end{frame}
\end{document}

enter image description here

1
  • Yes, that almost works as expected. The only (small) problem is that \phalt<+->{\phalt<+->{A}{B}}{C} does not work as nested \alt, likely due to the repeated typesetting, I guess. But I have no idea if that can be solved without an immense amount of work.
    – Bubaya
    Commented Dec 4, 2023 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .