# Changing elements of arrays in Beamer without shifting the unchanging elements.

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?

-
This seems very similiar to Beamer alt command like visible instead of like only. Have a look at my answer there which defines a \Alt macro suitable for math content. –  Martin Scharrer Mar 23 '11 at 8:52
@Eric: Also, in order to be able to edit/comment, please consider registering at tex.stackexchange. –  lockstep Mar 23 '11 at 16:41
[This was accidentally entered as an answer where I was trying instead to respond to Martin's comments.] Thank you @Martin. This piece of ingenuity seems to work brilliantly as a replacement for \alt wherever there are two alternatives. While I can't understand the TeX command definitions fully at my current level, I have little doubt that your solution could be tweaked with minimal effort to provide an environment-aware replacement for \temporal with three alternatives. The problem I have is more with using \alt and \temporal as the interface to your solution. –  Eric Weinstein Mar 23 '11 at 16:41
[cont.] Unfortunately, I have n items where n is small but greater than 3. How can I create \AltN for a list of n object to remove the seemingly unnecessarily limiting dependence on the two and three item interfaces inherited from \alt and \temporal? –  Eric Weinstein Mar 23 '11 at 16:41

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}


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

-
On second thought \temporalbox and \altbox might be better names. –  Martin Scharrer Mar 23 '11 at 18:24
Both the \altbox (in answer to the earlier question) and \temporalbox appear to work elegantly and flawlessly in every situation I faced where they have replace the \alt and \temporal commands. I have found that using \only within one of the temporal items curly brackets can extend the \temporalbox technique to cover more than three items at the cost of cluttering up the arguments. I hope Beamer will embrace this brilliant solution of @Martin's as it seems this problem is quite non-trivial to many people who use the package at my level. –  Eric Weinstein Mar 23 '11 at 20:49
@Eric: Thanks. I will submit these macros to the maintainers of beamer. –  Martin Scharrer Mar 23 '11 at 20:55

Martin's solution is excellent!

Unfortunatelly there is (at least) one situation, where \Temporal and \Alt do not behave as one might wish: Using it in an array, one might be interested in an array, where some lines shall be exchanged. \Temporal produces an error, whereas \temporal does not.

\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}

\begin{frame}<1-3>
$\left( \begin{array}{ccc} \temporal<2>{1 & 2 &3}{1 & 2 & 3\\4 & 5 & 6}{4 & 5 & 6} \end{array} \right)$
\end{frame}
\begin{frame}<1-3>
$\left( \begin{array}{ccc} \Temporal<2>{1 & 2 &3}{1 & 2 & 3\\4 & 5 & 6}{4 & 5 & 6} \end{array} \right)$
\end{frame}
\end{document}

-
Could you please explain the difference of your and Martin's solution? –  N.N. Nov 16 '11 at 17:18