为表格行添加超过 2 种颜色,每 (n) 行重新开始一次

为表格行添加超过 2 种颜色,每 (n) 行重新开始一次

这是我的第一个问题。MWE 提供了简短而详细的信息。

我想给表格的行着色,不只是交替使用 2 种颜色,而是一系列多种颜色,或者可能是 6 种灰色。*咳嗽*

请不要将此问题标记为与其他问题相似。我已经看到过这个: 每 n 行交替显示彩色行的表格

{tabu} 包目前无法运行。此处记录了有关此问题(rowcolors 包含超过 2 种颜色)的错误: https://github.com/tabu-issues-for-future-maintainer/tabu/issues/1

这是 MWE。我需要这样做来比较 4 个结果子集,例如,每个子集也由 6 个案例组成

\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}

可用功能与所需输出

我在网上找到的第一件事是使用命令\definecolorseries。但是,颜色序列会随着同一行内每次水平移动而增加。将循环大小设置为 .166666 而不是 1 没有帮助。最终你会得到所有颜色的 1 种颜色。

\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}

当表包含多列时 \definecolorseries 的输出

作为一名好学生,我在此展示我迄今为止使用该命令可以实现的功能\definecolorseries。我必须为每一列定义一个单独的系列,并单独使用它。这显然很麻烦。定义这么多新命令和变量并让它们到处乱飞并不是好的编码习惯。这篇文章的本质是(礼貌地)询问是否有人可以将其变成一些宏。特别是,非常有用的是能够定义一次颜色系列,并在不同的列中独立使用它。

\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}

临时解决方案 - 我的微薄之力


最后但同样重要的一点是,我在写这个问题时想到我可以定义一个新的计数器\newcommand{\MyCounter}{0}

然后,

  1. 在每一行的开头,我调用此命令:\rowcolor{blue!\MyCounter}
  2. 在每一行的末尾,我将计数器增加 15,或将其重置回 0 也许有一种方法可以自动执行此操作,而不是在每一行中手动输入这些命令。这个简单的解决方案还只允许相同颜色的不同色调。您不能将它用于 {黄色!50} 到 {蓝色!50}

多谢

答案1

在最新版本nicematrix(2021-08-10 的 v 6.0) 中,您有一个命令\rowlistcolors(可在 中使用\CodeBefore)专门用于解决此类问题。

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}    

\begin{document}

\begin{NiceTabular}{cccccc}
\CodeBefore 
  \rowlistcolors{2}{white,blue!05,blue!10,blue!15,blue!20,blue!25,blue!30}[restart]
\Body
  \toprule
  Input & Output 1 & Output 2 & Output 3 & Output 4 & Output 5 \\ \midrule
  \Block{1-*}{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-*}{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-*}{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}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容