3

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

  • How about a starter code? – user31729 Dec 19 '15 at 9:30
  • So, follow the second answer in your linked question. The solution is in the link there. – AboAmmar Dec 19 '15 at 12:17
  • @AboAmmar Thanks, I've updated questions. I've tried the way you mentioned. I'm still suffering from the unintended margin between the header and the 1st row... – rkjt50r983 Dec 20 '15 at 4:32
4

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.