[Tex/LaTex] Changing elements of arrays in Beamer without shifting the unchanging elements.

arraysbeameroverlays

An array is to be displayed in a Beamer presentation with some elements constant across all slides in the frame. Other array elements, however, must change with the overlay in the frame and are of non-constant height and width. It would seem desirable to reserve an unchanging amount of room for each variable array entry with something akin to \overlayarea or \overprint within the array itself, but this has so far failed to work. The commands which do work like \temporal end up changing the positioning of the entire array between slides.

Is there a preferred and/or elegant solution to this problem within Beamer?

Best Answer

Here the definition plus demonstration code of \Temporal. It works like \temporal but uses boxes like the \Alt suggested by me in Beamer alt command like visible instead of like only. In addition I added an optional argument which can be used to align the narrower alternatives to the left, middle or right ([l], [c] or [r]). This was necessary to support centered array cells.

\documentclass{beamer}
\usepackage{tikz}

\makeatletter
% Detect mode. mathpalette is used to detect the used math style
\renewcommand<>\Temporal[4][l]{%
    \begingroup
    \ifmmode
        \expandafter\mathpalette
        \expandafter\math@Temporal
    \else
        \expandafter\make@Temporal
    \fi
    {{#2}{#3}{#4}{#1}{#5}}%
    \endgroup
}

% Un-brace the second argument (required because \mathpalette reads the three arguments as one
\newcommand\math@Temporal[2]{\math@@Temporal{#1}#2}

% Set the three arguments in boxes. The math style is given by #1. \m@th sets \mathsurround to 0.
\newcommand\math@@Temporal[4]{%
    \setbox\z@ \hbox{$\m@th #1{#2}$}%
    \setbox\@ne\hbox{$\m@th #1{#3}$}%
    \setbox\tw@\hbox{$\m@th #1{#4}$}%
    \@Temporal
}

% Un-brace the argument
\newcommand\make@Temporal[1]{\make@@Temporal#1}

% Set the two arguments into normal boxes
\newcommand\make@@Temporal[3]{%
    \sbox\z@ {#1}%
    \sbox\@ne{#2}%
    \sbox\tw@{#3}%
    \@Temporal
}

% Place one of the two boxes using \rlap and place a \phantom box with the maximum of the two boxes
\newcommand\@Temporal[2]{%
    \setbox\@tempboxa\null
    \ht\@tempboxa
    \ifnum\ht\z@>\ht\@ne
        \ifnum\ht\z@>\ht\tw@
            \ht\z@
        \else
            \ht\tw@
        \fi
    \else
        \ifnum\ht\@ne>\ht\tw@
            \ht\@ne
        \else
            \ht\tw@
        \fi
    \fi
    \dp\@tempboxa
    \ifnum\dp\z@>\dp\@ne
        \ifnum\dp\z@>\dp\tw@
            \dp\z@
        \else
            \dp\tw@
        \fi
    \else
        \ifnum\dp\@ne>\dp\tw@
            \dp\@ne
        \else
            \dp\tw@
        \fi
    \fi
    \wd\@tempboxa
    \ifnum\wd\z@>\wd\@ne
        \ifnum\wd\z@>\wd\tw@
            \wd\z@
        \else
            \wd\tw@
        \fi
    \else
        \ifnum\wd\@ne>\wd\tw@
            \wd\@ne
        \else
            \wd\tw@
        \fi
    \fi
    % The \hfil's center the content. Remove one or both of them to right or left align them:
    \rlap{\hbox to \wd\@tempboxa{%
        \ifx l#1\else\hfil\fi
        \temporal#2%
            {\box\z@}%
            {\box\@ne}%
            {\box\tw@}%
        \ifx r#1\else\hfil\fi
    }}%
    \box\@tempboxa
}

\makeatother

\begin{document}

% Test the different modes and math styles
\begin{frame}<1-3>
Display:
\[
\begin{split}
\frac{dy}{dx} &= \left(\frac{2x}{x^2+1} + \frac{1}{2(x+3)} - \frac{1}{x-1}
\right)
\Temporal<2>{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z}.
\end{split}
\]

In-Text:
\(
\frac{dy}{dx} = \left(\frac{2x}{x^2+1} + \frac{1}{2(x+3)} - \frac{1}{x-1}
\right)
\Temporal<2>{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z}.
\)

Subscript:
\(
\frac{dy}{dx} = \left(\frac{2x}{x^2+1} + \frac{1}{2(x+3)} - \frac{1}{x-1}
\right) X_{\Temporal<2>{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z}}.
\)
\[
\frac{dy}{dx} = \left(\frac{2x}{x^2+1} + \frac{1}{2(x+3)} - \frac{1}{x-1}
\right) X_{\Temporal<2>{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z}}.
\]

Sub-Subscript:
\(
\frac{dy}{dx} = \left(\frac{2x}{x^2+1} + \frac{1}{2(x+3)} - \frac{1}{x-1}
\right) X_{X_{\Temporal<2>{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z}}}.
\)
\[
\frac{dy}{dx} = \left(\frac{2x}{x^2+1} + \frac{1}{2(x+3)} - \frac{1}{x-1}
\right) X_{X_{\Temporal<2>{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z}}}.
\]

Text-mode:
XXXX  \Temporal<2>{aaaaa}{Ag}{AAAA}.


\end{frame}
\begin{frame}<1-3>
    \[ \left(
    \begin{array}{ccc}
        \Temporal<2>[r]{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z} & 
        \Temporal<2>[c]{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z} & 
        \Temporal<2>[l]{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z} \\
        \Temporal<2>[l]{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z} & 
        \Temporal<2>[c]{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z} & 
        \Temporal<2>[r]{\frac{(x^2+1)\sqrt{x+3}}{x-1}}{y}{\int_x^y z} \\
        \Temporal<2>[c]{<2}{=2}{>2} &
        \Temporal<2>{\int x}{\iint x}{\oint x} \\
    \end{array}
        \right)
    \]

\end{frame}
\end{document}

Slide 1 Slide 2 Slide 3

(the size differences in the images is due to different cropping)