1

My first question here. MWE provided, short and detailed.

I want to color the rows of a table, not just alternating 2 colors, but a series of multiple colors, or perhaps 6 shades of grey. *cough*

Please do not mark this question as similar to another. I already saw this: Table with colored rows alternating every n rows

The {tabu} package isn't currently working. There is a documented bug on this issue (rowcolors with more than just 2 colors) here: https://github.com/tabu-issues-for-future-maintainer/tabu/issues/1

Here is a MWE. I need to do this to compare between 4 subsets of results for example, where each subset also consists of 6 cases

\documentclass[a4paper]{report}

\usepackage{multirow}
\usepackage[table]{xcolor}

\begin{document}
\begin{table}
\caption{The only thing that xcolor can provide as of now. Alternating row colors (2 colors only).}
\centering
\rowcolors{2}{cyan!65}{}
\begin{tabular}{
p{2cm}|         %1
p{1.75cm}||     %2
p{1.5cm}|       %3
p{1.5cm}|       %4
p{1.5cm}|       %5
p{1.5cm}|       %6
p{1.5cm}        %7
}
\hline \hline
Input 1 & Input 2 & Output 1 & Output 2 & Output 3 & Output 4 & Output 5 \\ \hline
\multirow{6}{*}{Group A} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group B} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group C} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline \hline
    \end{tabular}
\end{table}
%############################################################
\newpage
\begin{table}
\caption{What I want. any repeating sequence of 6 colors. 
6 shades of blue.
or any other series of 6 (i.e. red orange yellow green blue purple)
}
\centering
\begin{tabular}{
p{2cm}|         %1
p{1.75cm}||     %2
p{1.5cm}|       %3
p{1.5cm}|       %4
p{1.5cm}|       %5
p{1.5cm}|       %6
p{1.5cm}        %7
}
\hline \hline
Input 1 & Input 2 & Output 1 & Output 2 & Output 3 & Output 4 & Output 5 
\\ \hline\rowcolor{blue!0}
\multirow{6}{*}{Group A} & 
I   & 1  & 2  & 3&  3&  3 \\\rowcolor{cyan!12.5} &
II  & 4  & 5  & 6&  6&  6 \\\rowcolor{cyan!25} &
III & 7  & 8  & 9&  9&  9 \\\rowcolor{cyan!37.5} &
IIV & 10 & 11 & 12& 12& 12\\\rowcolor{cyan!50} &
V   & 10 & 11 & 12& 12& 12\\\rowcolor{cyan!62.5}&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group B} & 
I   & 1  & 2  & 3&  3&  3 \\\rowcolor{cyan!12.5} &
II  & 4  & 5  & 6&  6&  6 \\\rowcolor{cyan!25} &
III & 7  & 8  & 9&  9&  9 \\\rowcolor{cyan!37.5} &
IIV & 10 & 11 & 12& 12& 12\\\rowcolor{cyan!50} &
V   & 10 & 11 & 12& 12& 12\\\rowcolor{cyan!62.5}&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group C} & 
I   & 1  & 2  & 3&  3&  3 \\\rowcolor{cyan!12.5} &
II  & 4  & 5  & 6&  6&  6 \\\rowcolor{cyan!25} &
III & 7  & 8  & 9&  9&  9 \\\rowcolor{cyan!37.5} &
IIV & 10 & 11 & 12& 12& 12\\\rowcolor{cyan!50} &
V   & 10 & 11 & 12& 12& 12\\\rowcolor{cyan!62.5}&
VI  & 13 & 14 & 15& 15& 15\\ \hline \hline
    \end{tabular}
\end{table}
\end{document}

Available feature vs. Desired output

The first thing that I could find online is using the \definecolorseries command. However, the color sequence is incremented with every horizontal shift within the same row. Setting the cycle size to .166666 instead of 1 doesn't help. you end up with 1 color for all.

\documentclass[a4paper]{report}

\usepackage{multirow}
\usepackage[table]{xcolor}

\begin{document}
\begin{table}
\caption{To save you the time and effort, this is the default behavior of $\backslash$definecolorseries in a table with more than one column. The color is updated with every horizontal movement (every cell in a row). The xcolor package documentation does NOT touch upon this
}
\resetcolorseries[1]{test1}
\rowcolors[\hline]{1}{test1!!+}{test1!!+}
\begin{tabular}{%
p{2cm}   %  1
p{2cm}|| %  2
p{1.5cm} %  3
p{1.5cm} %  4
p{1.5cm} %  5
p{1.5cm} %  6
p{1.5cm} %  7
}
\hline \hline \rowcolor{white}
Input 1 & Input 2 & Output 1 & Output 2 & Output 3 & Output 4 & Output 5 
\\ \hline
\multirow{6}{*}{Group A} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group B} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group C} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline \hline
\end{tabular}
\end{table}
\end{document}

Output of \definecolorseries when the table has more than one column

Being a good student, I hereby show what I could achieve so far using the \definecolorseries command so far. I have to define a separate series for each column, and use it separately. This is obviously hectic. and it is not good coding practice to define this many new commands and variables, and leave them flying around. The essence of this post is to ask (nicely) if anybody could turn this into some Macro. Particularly, what would be very useful is to be able to define the color-series once, and use it independently in different columns.

\documentclass[a4paper]{report}

\usepackage{multirow}
\usepackage[table]{xcolor}

\begin{document}
\newpage
% if you want the sequence to repeat every (n) rows, choose numbers which are multiples of 1/n
% for example, 6 rows ===>> 1/6 = combinations of {+/-0.16666666,+/-0.166666666,+/-0.166666666}
\newcommand{\StartingColor}{.1666666666,1,1} %starting color
\newcommand{\FinishingColor}{-0.1666666,0,0} %step-size, in case of method=grad
\newcommand{\myMethod}{grad}%method
\newcommand{\myCycle}{1} %cycle length
\definecolorseries{test1}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test1}
\definecolorseries{test2}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test2}
\definecolorseries{test3}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test3}
\definecolorseries{test4}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test4}
\definecolorseries{test5}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test5}
\definecolorseries{test6}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test6}
\definecolorseries{test7}{rgb}{\myMethod}[rgb]{\StartingColor}[rgb]{\FinishingColor} \resetcolorseries[\myCycle]{test7}
\newcolumntype{P}[1]{>{\rule{-0.45pt}{0.275cm}\centering\arraybackslash}p{#1}}
\newcolumntype{\Pa}[1]{>{\columncolor{test1!!+}}p{#1}}
\newcolumntype{\Pb}[1]{>{\columncolor{test2!!+}}p{#1}}
\newcolumntype{\Pc}[1]{>{\columncolor{test3!!+}}p{#1}}
\newcolumntype{\Pd}[1]{>{\columncolor{test4!!+}}p{#1}}
\newcolumntype{\Pe}[1]{>{\columncolor{test5!!+}}p{#1}}
\newcolumntype{\Pf}[1]{>{\columncolor{test6!!+}}p{#1}}
\newcolumntype{\Pf}[1]{>{\columncolor{test6!!+}}p{#1}}
\newcolumntype{\Pg}[1]{>{\columncolor{test7!!+}}p{#1}}
\begin{table}
\caption{The best thing i could do so far using the {$\backslash$}definecolorseries command
And as you see, it is hectic, and not practical for large tables. 
Additionally, if i need to restart the series at some row, i have to enter the $\backslash$resetcolorseries command for all columns. Very difficult to implement other than basic colors. 
}
\begin{tabular}{%
\Pa{2cm}   %  1
\Pb{2cm}||  %  2
\Pc{1.5cm}   %  3
\Pd{1.5cm} %  4
\Pe{1.5cm}   %  5
\Pf{1.5cm}  %  6
\Pg{1.5cm}  %  7
}
\hline \hline \rowcolor{white}
Input 1 & Input 2 & Output 1 & Output 2 & Output 3 & Output 4 & Output 5 
\\ \hline\rowcolor{blue!0}
\multirow{6}{*}{Group A} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group B} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multirow{6}{*}{Group C} & 
I   & 1  & 2  & 3&  3&  3 \\&
II  & 4  & 5  & 6&  6&  6 \\&
III & 7  & 8  & 9&  9&  9 \\&
IIV & 10 & 11 & 12& 12& 12\\&
V   & 10 & 11 & 12& 12& 12\\&
VI  & 13 & 14 & 15& 15& 15\\ \hline \hline
\end{tabular}
\end{table}
%############################################################
\newpage
\begin{table}
\caption{The downside:I can't choose the starting row. 
}
\begin{tabular}{%
\Pb{2cm}|| %  1
\Pc{1.5cm} %  2
\Pd{1.5cm} %  3
\Pe{1.5cm} %  4
\Pf{1.5cm} %  5
\Pg{1.5cm} %  6
}
\hline \hline 
\rowcolor{white}
Input 2 & Output 1 & Output 2 & Output 3 & Output 4 & Output 5  \\
\rowcolor{white}
($^\circ$C) & (\$) & (MW) & (\%) & (cm) & (hr)
\\ \hline 
\multicolumn{6}{c}{Sub-Heading: Input 1 = A}\\ \hline
%you create a fake (0 height) line, and restart the color series of each column at the column itself
\resetcolorseries[\myCycle]{test2}&
\resetcolorseries[\myCycle]{test3}&
\resetcolorseries[\myCycle]{test4}&
\resetcolorseries[\myCycle]{test5}&
\resetcolorseries[\myCycle]{test6}&
\resetcolorseries[\myCycle]{test7}
\\[-1.2em] \hline \rowcolor{white} %without this, it starts from the last color
I   & 1  & 2  & 3&  3&  3 \\
II  & 4  & 5  & 6&  6&  6 \\
III & 7  & 8  & 9&  9&  9 \\
IIV & 10 & 11 & 12& 12& 12\\
V   & 10 & 11 & 12& 12& 12\\
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multicolumn{6}{c}{Sub-Heading: Input 1 = B}\\ \hline
I   & 1  & 2  & 3&  3&  3 \\
II  & 4  & 5  & 6&  6&  6 \\
III & 7  & 8  & 9&  9&  9 \\
IIV & 10 & 11 & 12& 12& 12\\
V   & 10 & 11 & 12& 12& 12\\
VI  & 13 & 14 & 15& 15& 15\\ \hline
\multicolumn{6}{c}{Sub-Heading: Input 1 = C}\\ \hline
I   & 1  & 2  & 3&  3&  3 \\
II  & 4  & 5  & 6&  6&  6 \\
III & 7  & 8  & 9&  9&  9 \\
IIV & 10 & 11 & 12& 12& 12\\
V   & 10 & 11 & 12& 12& 12\\
VI  & 13 & 14 & 15& 15& 15\\ \hline \hline
\end{tabular}
\end{table}
\end{document}

makeshift solution - my humble effort


Last but not least, it occurred to me just while writing this question is that I can define a new counter \newcommand{\MyCounter}{0}

then,

  1. at the beginning of every row, i call this command: \rowcolor{blue!\MyCounter}
  2. at the end of every row, i increment the counter by 15, or reset it back to 0 perhaps there could be a way to do this automatically, rather than enter these commands manually in every row. This simple solution also allows only different shades of the same color. you can't use it for {yellow!50} to {blue!50}

Thanks a lot

1

With {NiceTabular} of nicematrix, you have a key code-before and, with key, you can programmatically color the rows of your tabular. In the following code, I have used expl3 for the loop, but it would have been possible with other tools for TeX loops (e.g. pgffor).

\documentclass{article}

\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}

\ExplSyntaxOn
% The argument of \myrowcolors is the starting row 
\NewDocumentCommand \myrowcolors { m }
  {
    \int_step_inline:nnn { #1 } { \arabic{iRow} } 
      {
        \tl_set:Nx \l_tmpa_int % the color
          {
            \int_case:nn { \int_mod:nn { ##1 - #1 + 1 } 7 }
              {
                1 { white }
                2 { blue!05 }
                3 { blue!10 }
                4 { blue!15 }
                5 { blue!20 }
                6 { blue!25 }
                0 { blue!30 }
              }   
          }
        \exp_args:NV \rowcolor \l_tmpa_int { ##1 }
      }
  }
\ExplSyntaxOff

\begin{document}

\begin{NiceTabular}{CCCCCC}[code-before = \myrowcolors{2}]
\toprule
Input & Output 1 & Output 2 & Output 3 & Output 4 & Output 5 \\ \midrule
\Block{1-6}{Group A} \\
I   & 1  & 2  & 3&  3&  3 \\
II  & 4  & 5  & 6&  6&  6 \\
III & 7  & 8  & 9&  9&  9 \\
IV  & 10 & 11 & 12& 12& 12\\
V   & 10 & 11 & 12& 12& 12\\
VI  & 13 & 14 & 15& 15& 15\\ \midrule
\Block{1-6}{Group B} \\
I   & 1  & 2  & 3&  3&  3 \\
II  & 4  & 5  & 6&  6&  6 \\
III & 7  & 8  & 9&  9&  9 \\
IV  & 10 & 11 & 12& 12& 12\\
V   & 10 & 11 & 12& 12& 12\\
VI  & 13 & 14 & 15& 15& 15\\ \midrule
\Block{1-6}{Group C} \\
I   & 1  & 2  & 3&  3&  3 \\
II  & 4  & 5  & 6&  6&  6 \\
III & 7  & 8  & 9&  9&  9 \\
IV  & 10 & 11 & 12& 12& 12\\
V   & 10 & 11 & 12& 12& 12\\
VI  & 13 & 14 & 15& 15& 15\\ \bottomrule
\end{NiceTabular}

\end{document}

Output of the above code

| improve this answer | |
  • In this answer, since version 5.0 of nicematrix one must write cccccc instead of CCCCCC in the preamble of {NiceTabular} (but there is an option for backward compatibility). – F. Pantigny Jul 18 at 21:03
  • thanks. i tested the code and it works. As you mentioned, i need to use {cccccc} instead of {CCCCC} Can you please explain what's happening? the code is not simple even for an intermediate user like me. Also, how can i have two columns with different colors series. i.e: first column: 4 shades of blue, second column: 4 shades of green. Many thanks in advance – user3730981 Aug 15 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.