Is there a package or option that starts alternating row colors from within a table, so that no matter how many manually-colored rows are inserted, the alternating can be started again with the same color each time? (In a single table, I want to manually color a few "heading" rows, each followed by an undetermined number of alternating gray/white rows -- always starting with gray.)
2 Answers
Like this (change colors and alignment as You like)?
Code:
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{center}
\rowcolors{2}{gray!50}{white}
\begin{tabular}{lcr}
\rowcolor{red!50}
Table head1 & Table head1 & Table head1\\
\rowcolor{green!50}
Table head2 & Table head2 & Table head2\\
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
\end{tabular}
\end{center}
\end{document}
-
As I understand it, that will alternate gray/white starting from the beginning of the table, not provide a gray re-start after manually inserting a different color. As I described in my original question, I'm looking for a way to force the gray/white cycle to start anew (i.e., beginning with gray) mid-table. No package I've seen (at least those of which I could comprehend the options) will begin again on gray, unless it's an odd row. May 19 at 6:37
EDIT
With the latest version of nicematrix
(v. 6.21 2023-07-14), you have a command \rowcolors
which may be used in a row of the tabular.
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
The command \verb|\myrowcolors{gray!50}{white}| is in the third row.
\begin{center}
\begin{NiceTabular}{lcr}[colortbl-like]
\rowcolor{red!50}
Table head1 & Table head1 & Table head1\\
\rowcolor{green!50}
Table head2 & Table head2 & Table head2\\
\rowcolors{gray!50}{white}%
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
\end{NiceTabular}
\end{center}
\bigskip
The command \verb|\myrowcolors{gray!50}{white}| is in the fourth row.
\begin{center}
\begin{NiceTabular}{lcr}[colortbl-like]
\rowcolor{red!50}
Table head1 & Table head1 & Table head1\\
\rowcolor{green!50}
Table head2 & Table head2 & Table head2\\
odd & odd & odd \\
\rowcolors{gray!50}{white}
even & even & even\\
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
\end{NiceTabular}
\end{center}
\end{document}
The package nicematrix
does not provide such feature for its environment {NiceTabular}
but it's possible to program it.
\documentclass{article}
\usepackage{nicematrix,tikz}
\ExplSyntaxOn
\makeatletter
\cs_new_protected:Nn \__pantigny_rowcolors:nnn
{
\int_step_inline:nnn { #1 } { \int_use:N \c@iRow }
{
\tikz \fill [color= \int_if_even:nTF { ##1 - #1 } { #2 } { #3 } ]
(##1-|1) rectangle (\int_eval:n { ##1 + 1 } -|last) ;
}
}
\NewDocumentCommand { \myrowcolors } { m m }
{
\tl_gput_right:Nx \g_nicematrix_code_before_tl
{ \__pantigny_rowcolors:nnn { \int_use:N \c@iRow } { #1 } { #2 } }
}
\makeatother
\ExplSyntaxOff
\begin{document}
The command \verb|\myrowcolors{gray!50}{white}| is in the third row.
\begin{center}
\begin{NiceTabular}{lcr}[colortbl-like]
\rowcolor{red!50}
Table head1 & Table head1 & Table head1\\
\rowcolor{green!50}
Table head2 & Table head2 & Table head2\\
\myrowcolors{gray!50}{white}%
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
\end{NiceTabular}
\end{center}
\bigskip
The command \verb|\myrowcolors{gray!50}{white}| is in the fourth row.
\begin{center}
\begin{NiceTabular}{lcr}[colortbl-like]
\rowcolor{red!50}
Table head1 & Table head1 & Table head1\\
\rowcolor{green!50}
Table head2 & Table head2 & Table head2\\
odd & odd & odd \\
\myrowcolors{gray!50}{white}%
even & even & even\\
odd & odd & odd \\
even & even & even\\
odd & odd & odd \\
even & even & even\\
\end{NiceTabular}
\end{center}
\end{document}
colortbl
or/andxcolor
package. they offer commands for coloring rows, columns and cells.