Beamer overlay with `nicematrix`

beamernicematrixoverlaystables

I'd like to uncover a table row-by-row in a NiceTabular environment (provided by the nicematrix package). My table has alternating row colors, and I'd like overlay to cover/uncover the row color and its content at the same time. I'd also like to stick to the nicematrix package instead of using the conventional tabular/array environments.

I tried several ways that work in tabular and similar environments, but not in NiceTabular. These include:

MWE

\documentclass{beamer}
\usepackage{nicematrix,booktabs}

\begin{document}
\begin{frame}
\centering
\begin{NiceTabular}{m{.4\textwidth}m{.4\textwidth}}
  \CodeBefore
    \rowcolor{gray!50}{1} % heading
    \rowcolors{2}{gray!25}{} % body
  \Body
  \toprule
  \onslide<2->{Head 1 & Head 2} \\
  \midrule
  Row 1 Col 1 & Row 1 Col 2 \pause\\
  Row 2 Col 1 & Row 2 Col 2 Row 2 Col 2 Row 2 Col 2 Row 2 Col 2 \\
  \bottomrule 
\end{NiceTabular}

Overlay in conventional \texttt{tabular} for comparison:
\begin{tabular}{m{.4\textwidth}m{.4\textwidth}}
  \toprule
  \onslide<2->{Head 1 & Head 2} \\
  \midrule
  Row 1 Col 1 & Row 1 Col 2 \pause\\
  Row 2 Col 1 & Row 2 Col 2 Row 2 Col 2 Row 2 Col 2 Row 2 Col 2 \\
  \bottomrule 
\end{tabular}
\end{frame}
\end{document}

Best Answer

Here is what I would do.

\documentclass{beamer}
\usepackage{nicematrix,booktabs}

\begin{document}
\begin{frame}[t]
\centering
\begin{NiceTabular}{m{.4\textwidth}m{.4\textwidth}}
\CodeBefore
  \rowcolor{gray!50}{1}
  \rowcolors{2}{gray!25}{}
\Body
\toprule
  Head 1 & Head 2 \\
\midrule
\only<2->
  { Row 1 Col 1 & text text text text text text text text text text }
\only<3->
  { \\ Row 2 Col 1 & text text text text text }
\only<4->
  { \\ Row 3 Col 1 & text text text text text text text text text }
\only<5->
  {
    \\ Row 4 Col 1 & text text \\
    \bottomrule
  }
\end{NiceTabular}
\end{frame}
\end{document}

As usual with nicematrix, you need several compilations.


If you accept to write in white the text that must be hidden, you have also the following solution (with a centered vertical position of the tabular).

\documentclass{beamer}
\usepackage{nicematrix,booktabs}

\begin{document}
\begin{frame}
\centering
\color{white}
\begin{NiceTabular}{m{.4\textwidth}m{.4\textwidth}}[rules/color=black]
\CodeBefore
  \rowcolor{gray!50}{1}
\Body
\toprule
  \RowStyle[color=black]{}
  Head 1 & Head 2 \\
\midrule
\only<2->{\RowStyle[color=black,rowcolor=gray!25]{}}
  Row 1 Col 1 & text text text text text text text text text text \\ 
\only<3->{\RowStyle[color=black]{}}
  Row 2 Col 1 & text text text text text \\ 
\only<4->{\RowStyle[color=black,rowcolor=gray!25]{}}
  Row 3 Col 1 & text text text text text text text text text \\ 
\only<5->{\RowStyle[color=black]{}}
  Row 4 Col 1 & text text \\
\bottomrule
\end{NiceTabular}
\end{frame}
\end{document}
Related Question