[Tex/LaTex] Cell overlay in beamer

beameroverlaystables

I used the code posted in this thread to highlight cells in a table:

\documentclass[xcolor=table]{beamer}

\rowcolors{1}{gray!30}{gray!10}

\makeatletter
\def\rowcolor{\noalign{\ifnum0=`}\fi\bmr@rowcolor}
\newcommand<>{\bmr@rowcolor}{%
    \alt#1%
        {\global\let\CT@do@color\CT@@do@color\@ifnextchar[\CT@rowa\CT@rowb}% 
        {\ifnum0=`{\fi}\@gooble@rowcolor}% 
}

\newcommand{\@gooble@rowcolor}[2][]{\@gooble@rowcolor@}
\newcommand{\@gooble@rowcolor@}[1][]{\@gooble@rowcolor@@}
\newcommand{\@gooble@rowcolor@@}[1][]{\ignorespaces}
\makeatother



\makeatletter
\def\cellcolor{{\ifnum0=`}\fi\bmr@cellcolor}
\newcommand<>{\bmr@cellcolor}{%
    \alt#1%
        {\global\let\CT@do@color\CT@@do@color\@ifnextchar[\CT@rowa\CT@rowb}% 
        {\ifnum0=`{\fi}\@gooble@cellcolor}% 
}

\newcommand{\@gooble@cellcolor}[2][]{\@gooble@cellcolor@}
\newcommand{\@gooble@cellcolor@}[1][]{\@gooble@cellcolor@@}
\newcommand{\@gooble@cellcolor@@}[1][]{\ignorespaces}
\makeatother


\begin{document}
\begin{frame}{The MWE}%

\only<2>{\rowcolors{1}{blue!30}{blue!10}}
\only<1,3>{\rowcolors{1}{gray!30}{gray!10}}

\begin{center}
\begin{tabular}{cc}
    A & B \\
    A & B \\
    A & B \\
    \rowcolor<4>{green} C & D \\
    \rowcolor<4,5>{yellow} E & F \\
    \rowcolor<4-6>{green} G & H \\
    \rowcolor<6>{red} Y & S \\
    A & B \\

\end{tabular}
\end{center}

\par    
\visible<1>{Testing default row colouring ... \\}
\visible<2,3>{Testing change of default colors ...\\}
\visible<4-6>{Testing in-out of custom colors ...\\ (caution: The order of defaults colors can change)\\}

\vfill
\scriptsize{
 Based of answer of Martin Scharrer
 \url{https://tex.stackexchange.com/questions/18427/why-cant-i-wrap-rowcolor-in-only-beamer}}

\end{frame} 

\begin{frame}{Cell Coloring with In-out Effects}
   \begin{center}
   \begin{tabular}{cc}
    A & B \\
    C & \cellcolor<3>{green} C \\
    D & E \\
    F & G \\

    \end{tabular}
    \end{center}
    \end{frame}

\end{document}

But if I change the last frame to

\begin{frame}{Cell Coloring with In-out Effects}
\begin{center}
\begin{tabular}{cc}
A & B \\
\cellcolor<3>{green} C &  C \\
D & E \\
F & G \\

\end{tabular}
\end{center}
\end{frame}

then the whole second row gets highlighted. Can somebody tell me how to redefine \cellcolor above to make sure that only one cell will be highlighted?

Best Answer

A simple redefinition can make \cellcolor overlay-aware:

\documentclass[xcolor=table]{beamer}
\renewcommand<>\cellcolor[1]{\only#2{\beameroriginal\cellcolor{#1}}}

\begin{document}

\begin{frame}

   \begin{tabular}{cc}
        A & B \\
        \cellcolor{green}C &  C \\
        D & E \\
        \cellcolor<2>{red}F & G \\
    \end{tabular}

\end{frame}

\end{document}

enter image description here