# The rowcolor command for table is changing colors of the matrix rows as well

I am preparing a beamer presentation where I want to use colored rows in a table, for which I am using \rowcolors{2}{green!20}{blue!20} in the preamble of latex code.

The command works fine for table however, somehow it affects the pmatrix as well (shown below). Is there any way to avoid it? I want the matrix with plain background.

\documentclass[xcolor=table,10pt]{beamer}
\usetheme{Frankfurt}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\rowcolors{2}{green!20}{blue!20}

\begin{document}
\begin{frame}
\begin{tabular}{cc}
\hline
\rowcolor{yellow} Column 1  & Column 2 \\
\hline
23      & 234 \\
234     & 56  \\
\hline
\end{tabular}

$$\lambda= \left( \begin{matrix} \lambda_1 & 0 &\hdots & 0 \\ 0 & \lambda_2 &\hdots & 0 \\ 0 & 0 & \ddots & 0 \\ 0 & 0 & \hdots &\lambda_n \end{matrix} \right)$$.
\end{frame}
\end{document}


Edit 1: After the @Warner's suggestion I tried defining \rowcolors command within the Table environment and it works. It no longer affects the matrix.

Warner's approach requires me to add some extra lines, each time I include a new table. I am preparing a set of lectures where I would like to have a global setting (if possible). It will reduce code length as well as make it consistent throughout.

• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Jul 22, 2021 at 16:37
• Well, arrays and tabulars are inherently similar, so the fact that \rowcolors spills over makes sense. If you want a localised change, you may have to consider adding \rowcolors only where you want it, or to issue \rowcolors (with a local scope/within a group) with appropriate row colours that differ from the global scope colours.
– Werner
Jul 22, 2021 at 17:50
• Does your presentation contain other tables as well? If so, do you want \rowcolors to also apply to them? Jul 22, 2021 at 18:15
• @Warner advice helped. I still look for a global setting/solution, so appended this requirement in the question. Jul 22, 2021 at 19:12
• @leandriis yes It is actually a set of lectures containing many tables and I would like to have a consistent appearance for all tables. Jul 22, 2021 at 19:12

It’s easy peasy with tblr environment of the new LaTeX3 package tabularray:

\documentclass[10pt]{beamer}

\usetheme{Frankfurt}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\usepackage{tabularray}
\SetTblrInner{row{odd}={blue9},row{even}={green9},row{1}={yellow9}}

\begin{document}

\begin{frame}

\begin{tblr}{cc}
\hline
Column 1  & Column 2 \\
\hline
23      & 234 \\
234     & 56  \\
\hline
\end{tblr}

$$\lambda= \left( \begin{matrix} \lambda_1 & 0 &\hdots & 0 \\ 0 & \lambda_2 &\hdots & 0 \\ 0 & 0 & \ddots & 0 \\ 0 & 0 & \hdots &\lambda_n \end{matrix} \right)$$.

\end{frame}

\end{document}


I saw your post and had exactly the same problem, just solved it for my file. I highly recommend you to create a new table environment where you can configure ONLY such table.

(Because of [H] , \usepackage{float} is required)

\documentclass[xcolor=table,10pt]{beamer}
\usetheme{Frankfurt}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\usepackage{float}

%------------Custom table--------------------------------------
\newenvironment{MyTabular}[1]{% #1 = column types
\setlength{\intextsep}{3pt} %Space between text and table
\rowcolors{2}{green!20}{blue!20} %Color config
\begin{table}[H]
\centering
\renewcommand{\arraystretch}{1.1}
\begin{tabular}{#1} \rowcolor{yellow} %First row color
}{%
\end{tabular}
\end{table}
}
%--------------------------------------------------------------

\begin{document}
\begin{frame}
\begin{MyTabular}{cc}
\hline
Column 1  & Column 2 \\
\hline
23      & 234 \\
234     & 56  \\
\hline
\end{MyTabular}

$$\lambda= \left( \begin{matrix} \lambda_1 & 0 &\hdots & 0 \\ 0 & \lambda_2 &\hdots & 0 \\ 0 & 0 & \ddots & 0 \\ 0 & 0 & \hdots &\lambda_n \end{matrix} \right)$$.
\end{frame}
\end{document}


• This is the global answer we were looking for ;) Jul 24, 2021 at 16:16

You may try the following;

\documentclass[xcolor=table,10pt]{beamer}
\usetheme{Frankfurt}
\usepackage{amsmath,amssymb}

\usepackage{etoolbox}        % <---
\AtBeginEnvironment{tabular}{% <---
\rowcolors{2}{green!20}{blue!20}
}

\begin{document}
\begin{frame}
\frametitle{Coloring only \texttt{tabular}s}
\begin{tabular}{cc}
\hline
\rowcolor{yellow} Column 1  & Column 2 \\
\hline
23      & 234 \\
234     & 56  \\
\hline
\end{tabular}

$\lambda= \begin{pmatrix} \lambda_1 & 0 &\hdots & 0 \\ 0 & \lambda_2 &\hdots & 0 \\ 0 & 0 & \ddots & 0 \\ 0 & 0 & \hdots &\lambda_n \end{pmatrix}$.
\end{frame}
\end{document}


If you gave other tables' environments, you can add coloring definitions also for them.