[Tex/LaTex] Adding color to a table row as a beamer overlay

beamercolortables

I'm trying to have an overlay that adds a background color to a row of a table in beamer.

This post showed me how to do that for column. How about row?

Adding color to a table column as a beamer overlay

Update

Thanks to the comment by @AboAmmar, I followed the way described in the link. However, the unintended space appears between the header and the first row when the first row is highlighted. Similarly, the bottom row also suffers from the unintended blank margin.

My code is (Only add caption and top/mid rules):

\begin{tabular}{p{.455\linewidth}p{.455\linewidth}}%
\toprule
a & b \\
\midrule
\only<1-2,5>{\\}%
\only<3-4>{\\\rowcolor{cyan!25}}A & B\only<1-2,5>{\\}%
\only<3-4>{\\\rowcolor{cyan!25}}C & D\only<1,5>{\\}%
\only<2-4>{\\\rowcolor{lime!25}}E & F\only<1,5>{\\}%
\only<2-4>{\\\rowcolor{lime!25}}G & H\only<1>\\%
\only<2-4>{\\\rowcolor{lime!25}}\only<5>{\\\rowcolor{red!35}}I & J\only<1-3,5>{\\}%
\only<4-4>{\\\rowcolor{orange!25}}K & L\only<1-2,5>{\\}%
\only<3-4>{\\\rowcolor{cyan!25}}M & N\only<1-3,5>{\\}%
\only<4-4>{\\\rowcolor{orange!25}}O & P\only<1-3>{\\}%
\only<4-4>{\\\rowcolor{orange!25}}\only<5>{\\\rowcolor{red!35}}Q & R\only<1-2,5>{\\}%
\only<3-4>{\\\rowcolor{cyan!25}}S & T%

\end{tabular}

enter image description here

Best Answer

In general it's a bit trickier to colour the row, as \rowcolor has to be the first element within the row. So, while \rowcolor{.} works, using a selective (or overlay) \only<..>{\rowcolor{.}} makes \only the first element in the row.

As such, an approach using \only<..>{\\\rowcolor{.}} ensures \rowcolor is the first entry in the row. You can correct for the mis-aligned (or empty) rows by using \\[-\normalbaselineskip]:

enter image description here

\documentclass{beamer}

\usepackage{booktabs,colortbl}

\begin{document}

\begin{frame}
  \centering
  \begin{tabular}{ p{.455\linewidth} p{.455\linewidth} }%
  \toprule
  a & b \\
  \midrule
  \strut\\[-\normalbaselineskip]%
  \only<3-4>{\\[-\normalbaselineskip]\rowcolor{cyan!25}}A & B\only<1-2,5>{\\}%
  \only<3-4>{\\\rowcolor{cyan!25}}C & D\only<1,5>{\\}%
  \only<2-4>{\\\rowcolor{lime!25}}E & F\only<1,5>{\\}%
  \only<2-4>{\\\rowcolor{lime!25}}G & H\only<1>\\%
  \only<2-4>{\\\rowcolor{lime!25}}\only<5>{\\\rowcolor{red!35}}I & J\only<1-3,5>{\\}%
  \only<4-4>{\\\rowcolor{orange!25}}K & L\only<1-2,5>{\\}%
  \only<3-4>{\\\rowcolor{cyan!25}}M & N\only<1-3,5>{\\}%
  \only<4-4>{\\\rowcolor{orange!25}}O & P\only<1-3>{\\}%
  \only<4-4>{\\\rowcolor{orange!25}}\only<5>{\\\rowcolor{red!35}}Q & R\only<1-2,5>{\\}%
  \only<3-4>{\\\rowcolor{cyan!25}}S & T%
\end{tabular}
\end{frame}

\end{document}