7

I would like to know how to highlight rows within a table in beamer, piecewise, so that when I present the piece, I can originally have no rows highlighted, and then the first, and then both the first and the second, etc.

I have the following table, with the first two rows highlighted:

\begin{frame}
\begin{table}
\begin{center}    
\begin{tabular}{lcc|ccc}
\multicolumn{6}{c}{Effect of Mandate on Fertility by Age: Employed Individuals} \\ \hline \hline
 & (1) & (2) & (3) & (4) & (5) \\
 & Has Child & Has Child LT 5 & Has Child LT 5 & Has Child LT 5 & Has Child LT 5 \\
VARIABLES & All & All & No HS Degree & HS or College & Masters or More \\ \hline
 &  &  &  &  &  \\
\rowcolor{LRed} Married Women 18-20*& -0.11 & -0.08* & -0.21*** & -0.00 & 0.00 \\
\rowcolor{LRed} After*Treatment State & [0.071] & [0.045] & [0.069] & [0.055] & [0.000]     \\
 &  &  &  &  &  \\
\rowcolor{LRed} Married Women 20-25*& -0.07*** & -0.07*** & -0.04 & -0.08*** & -0.38* \\
\rowcolor{LRed} After*Treatment State & [0.023] & [0.015] & [0.036] & [0.016] & [0.215] \\
 &  &  &  &  &  \\
Married Women 25-30*& -0.00 & 0.03** & 0.19*** & 0.01 & 0.11* \\
After*Treatment State & [0.019] & [0.012] & [0.032] & [0.014] & [0.058] \\
 &  &  &  &  &  \\
Married Women 30-35*& 0.01 & -0.01 & -0.09*** & -0.01 & 0.12** \\
After*Treatment State & [0.020] & [0.012] & [0.029] & [0.014] & [0.051] \\
 &  &  &  &  &  \\
Married Women 35-40*& -0.01 & -0.02** & -0.07*** & -0.03* & 0.10* \\
After*Treatment State & [0.019] & [0.012] & [0.026] & [0.014] & [0.053] \\
 &  &  &  &  &  \\
Observations & 152,366 & 152,366 & 34,081 & 110,324 & 7,961 \\
R-squared & 0.38 & 0.24 & 0.28 & 0.24 & 0.17 \\
 Mean of Dependant Variable & 0.48 & 0.09 & 0.07 & 0.10 & 0.08 \\ \hline \hline
\multicolumn{6}{c}{ Standard errors in brackets} \\
\multicolumn{6}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}
}
\end{center}
\end{table}
\end{frame}
1

2 Answers 2

13

The solution linked in the comments is the most appropriate, but one might also use the hf-tikz package (with a bit of abstraction as it's not the main purpose of the package).

A possible implementation:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage[beamer,customcolors]{hf-tikz}

\tikzset{hl/.style={
    set fill color=red!80!black!40,
    set border color=red!80!black,
  },
}

\begin{document}

\begin{frame}{Title}
\begin{center}
\resizebox{\textwidth}{!}{
\begin{tabular}{lcc|ccc}
\multicolumn{6}{c}{Effect of Mandate on Fertility by Age: Employed Individuals} \\ \hline \hline
 & (1) & (2) & (3) & (4) & (5) \\
 & Has Child & Has Child LT 5 & Has Child LT 5 & Has Child LT 5 & Has Child LT 5 \\
VARIABLES & All & All & No HS Degree & HS or College & Masters or More \\ \hline
 &  &  &  &  &  \\
\tikzmarkin<2->[hl]{a} Married Women 18-20*& -0.11 & -0.08* & -0.21*** & -0.00 & 0.00 \\
 After*Treatment State & [0.071] & [0.045] & [0.069] & [0.055] & [0.000] \tikzmarkend{a}    \\
 &  &  &  &  &  \\
\tikzmarkin<3->[hl]{b} Married Women 20-25*& -0.07*** & -0.07*** & -0.04 & -0.08*** & -0.38* \\
 After*Treatment State & [0.023] & [0.015] & [0.036] & [0.016] & [0.215] \tikzmarkend{b} \\
 &  &  &  &  &  \\
Married Women 25-30*& -0.00 & 0.03** & 0.19*** & 0.01 & 0.11* \\
After*Treatment State & [0.019] & [0.012] & [0.032] & [0.014] & [0.058] \\
 &  &  &  &  &  \\
Married Women 30-35*& 0.01 & -0.01 & -0.09*** & -0.01 & 0.12** \\
After*Treatment State & [0.020] & [0.012] & [0.029] & [0.014] & [0.051] \\
 &  &  &  &  &  \\
Married Women 35-40*& -0.01 & -0.02** & -0.07*** & -0.03* & 0.10* \\
After*Treatment State & [0.019] & [0.012] & [0.026] & [0.014] & [0.053] \\
 &  &  &  &  &  \\
Observations & 152,366 & 152,366 & 34,081 & 110,324 & 7,961 \\
R-squared & 0.38 & 0.24 & 0.28 & 0.24 & 0.17 \\
 Mean of Dependant Variable & 0.48 & 0.09 & 0.07 & 0.10 & 0.08 \\ \hline \hline
\multicolumn{6}{c}{ Standard errors in brackets} \\
\multicolumn{6}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}
}
\end{center}
\end{frame}

\end{document}

The result:

enter image description here

A remark: the \resizebox has been adopted as the table does not fit the frame. It comes from graphicx, a package automatically loaded by Beamer, so no need of load it explicitly.

4
  • I really like the quality of the result, but that extra space inserted at the beginning of the marked lines is tickling my OCD. Is that caused by beamer uncovering or hf-tikz?
    – levesque
    Feb 21, 2016 at 18:01
  • Okay, answered my own question. It is caused by tikzmarkin, and can be fixed by appending the tikzmarkin command with \unskip. tex.stackexchange.com/questions/280198/beamer-annotated-tables/…
    – levesque
    Feb 21, 2016 at 18:05
  • The result is brilliant. How can I remove the highlight of a row before highlighting another one ?
    – Irazza
    Jul 13, 2022 at 18:15
  • @Irazza: just remove the -, i.e., say \tikzmarkin<2>. Jul 14, 2022 at 18:27
3

In an effort to complete this question, I borrowed the answer of fran as this seems to be a relative simple solution to the present problem

\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

\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} A & B \\
    \rowcolor<4,5>{yellow}A & B \\
    \rowcolor<4-6>{green}A & B \\
    \rowcolor<6>{red} A & B \\
    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}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .