4

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
  • See colortbl or/and xcolor package. they offer commands for coloring rows, columns and cells.
    – Zarko
    May 18 at 5:56
  • @Zarko, I'm not yet seeing anything in those that will fulfill the requirements. Please see my clarification in my reply to Raffaele Santoro, below. (I'm looking to re-start on gray anywhere in the midst of a table, not simply on an odd row.)
    – user182238
    May 19 at 6:48

2 Answers 2

9

Like this (change colors and alignment as You like)?

enter image description here

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}
1
  • 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.
    – user182238
    May 19 at 6:37
0

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}

Output of the first code


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}

Output of the second code

You must log in to answer this question.

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