建议不使用彩色表格包来为多行表着色

建议不使用彩色表格包来为多行表着色

我有这个代码:

\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\usepackage{amssymb}
\usepackage[table]{xcolor}

\usepackage{booktabs}

\begin{document}


{\centering
\begin{tabular}{>{\centering\arraybackslash}m{2cm} >{\centering\arraybackslash}m{3cm}>{\centering\arraybackslash}m{4cm}}
\arrayrulecolor{red}\toprule
\rowcolor{gray!60}Something & Number & \begin{tabular}{c}Something \\(splitted)\end{tabular}\\\midrule
\rowcolor{gray!20}\cellcolor{gray!60} & 1 & Here is a sentence automatically breakad\\
\rowcolor{gray!40}\cellcolor{gray!60}&2 & 2 secs  \\
\rowcolor{gray!20}\cellcolor{gray!60}&3 & 2 secs  \\
\rowcolor{gray!40}\cellcolor{gray!60}&4 & 2 secs  \\
\rowcolor{gray!20}\cellcolor{gray!60}&5 & 2 secs \\
\rowcolor{gray!40}
\multirow{-6}{*}{\cellcolor{gray!60}Multirow}&6 & 2 secs\\\bottomrule 
\end{tabular}

\end{document}

重现了该表:

在此处输入图片描述

我该如何解决水平线问题:1)它们根本不会出现在“多行”彩色单元格内 2)它们不会“破坏”我的红色线条?

我不想使用tabutikz或类似的解决方案......而只是寻找具有常规表格环境的方法。

我正在考虑给这些细线着色并让它们变得更厚,但我不知道该怎么做。

有什么建议么?

编辑:我还想保留列之间的水平线(几乎不可见),如果可能的话,还想保留其他行之间的水平线(除了“多行”单元格内和红线周围的线)。如果我不能保留它们,至少要通过\cline{}或其他方式重新创建它们(但垂直线也一样)。

我最初想到的解决方案是创建一些颜色\clines,但没有找到有关如何操作的信息。如果还有其他解决方案和建议,我将不胜感激。

非常感谢@Bernard 的回答,但我遇到了以下问题:

(B1)列与行之间的分隔线用于分隔不同的单元格(如下图第 3、4、5 行之间的分隔线以及列与列之间的分隔线):

\toprule(B2) 、\midrule\botomrule(红线)周围的颜色仍然是白色。

在此处输入图片描述

上图仍然有上面的问题 1 和 2 [=(B2)],但没有 (B1)

答案1

这是一个 hack,使用 的可选参数\\和 的悬垂值\rowcolor

\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\usepackage{amssymb}
\usepackage[table]{xcolor}

\usepackage{booktabs, makecell}
\newcommand{\myrowcolor}[1]{\rowcolor{#1}[\tabcolsep][\dimexpr\tabcolsep+0.2pt]}

\begin{document}

{\centering\setlength{\extrarowheight}{2pt}
\begin{tabular}{>{\centering\arraybackslash}m{2cm} >{\centering\arraybackslash}m{3cm}>{\centering\arraybackslash}m{4cm}}
\arrayrulecolor{red}\toprule
\myrowcolor{gray!60} Something & Number & \makecell{Something \\(splitted)}\\\midrule
\myrowcolor{gray!20}\cellcolor{gray!60} & 1 & Here is a sentence automatically breakad\\[-0.2pt]
\myrowcolor{gray!40} \cellcolor{gray!60}&2 & 2 secs \\[-0.2pt]
\myrowcolor{gray!20} \cellcolor{gray!60}&3 & 2 secs \\[-0.2pt]
\myrowcolor{gray!40} \cellcolor{gray!60}&4 & 2 secs \\[-0.2pt]
\myrowcolor{gray!20} \cellcolor{gray!60}&5 & 2 secs \\[-0.2pt]
\myrowcolor{gray!40}
\multirow{-6.5}{=}{\cellcolor{gray!60}Multirow}&6 & 2 secs\\\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

但是,我认为由于这些规则(\aboverulesep和)的填充,booktabs 规则周围的白色条纹看起来不太好看。删除此填充并稍微增加以进行补偿\belowrulesep会更好。\arraystretch

编辑

以下是去除 booktabs 规则周围白色条纹的解决方案:我将这些规则的填充设置为,并将其替换为在以字母为前缀的说明符的列中的所有单元格顶部和底部0pt定义的最小填充(或者如果您加载)。我还引入了行之间的灰色分隔规则:cellspaceSCsiunitx

\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\usepackage{amssymb}
\usepackage[table]{xcolor}

\usepackage{booktabs, makecell, cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{3pt}
\newcommand{\myrowcolor}[1]{\rowcolor{#1}[\tabcolsep][\dimexpr\tabcolsep+0.2pt]}
\newcommand{\myhhline}{\hhline{>{\arrayrulecolor{gray!60}}---}}


\begin{document}

{\centering
\setlength{\aboverulesep}{-0.1pt}
\setlength{\belowrulesep}{-0.1pt}
    \begin{tabular}{>{\centering\arraybackslash}m{2cm} >{\centering\arraybackslash}m{3cm}>{\centering\arraybackslash}S{m{4cm}}}
    \arrayrulecolor{red}\toprule
    \rowcolor{gray!60} Something & Number & \makecell{Something \\(splitted)}\\\midrule
    \rowcolor{gray!20}\cellcolor{gray!60} & 1 & Here is a sentence automatically breakad\\[-0.4pt]
\arrayrulecolor{gray!60}\midrule
 \rowcolor{gray!40} \cellcolor{gray!60}&2 & 2 secs \\[-0.4pt]
 \midrule
    \rowcolor{gray!20} \cellcolor{gray!60}&3 & 2 secs \\[-0.4pt]
\midrule
    \rowcolor{gray!40} \cellcolor{gray!60}&4 & 2 secs \\[-0.4pt]
\midrule
    \rowcolor{gray!20} \cellcolor{gray!60}&5 & 2 secs \\[-0.4pt]
\midrule

\rowcolor{gray!40}
\multirow{-8}{=}{\cellcolor{gray!60}Multirow}&6 & 2 secs\\ 
\arrayrulecolor{red}\bottomrule
\end{tabular}

\end{document} 

在此处输入图片描述

答案2

仅展示一些使用 tabu 的结果,因为垂直细线没有得到解决,请不要将此视为强加该软件包,但我认为 tabu 的设计只是为了解决这些问题,作为 tabularx 的扩展,因为它专注于处理颜色,当然这并不容易,也不够,它也有它的问题,但它产生了以下结果。

结果: 在此处输入图片描述

梅威瑟:

% arara: pdflatex: {synctex: yes, action: nonstopmode}
\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{booktabs}
\usepackage{amssymb}


%Packages for beautifull tables.
\usepackage{tabu}
\usepackage{array}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{makecell}
\usepackage{lipsum} % Dummy text


\begin{document}
Some initial text.\\
\begin{table}[!h]
    \def\TableLineSize{1.5pt}
    \small
    \tabulinesep =1.5pt 
    \tabulinestyle{\TableLineSize red}
    \taburulecolor|red|{white}
    \arrayrulewidth=1mm \doublerulesep=1mm
    \taburowcolors[2] 2{gray!25 .. gray!50}
    \begin{tabu} to \linewidth {X[1.2mc]X[1,mc]X[2.2,mc]} \firsthline\hline
        \rowcolor{gray!60}
        Something & Number & \makecell{Something \\(splitted)}\\ \firsthline\hline
        \cellcolor{gray}& 1 & Here is a sentence automatically breaked \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \multirow{-10}{*}{\cellcolor{gray}Multirow}
        & 1 & 2 secs  \\ \firsthline\hline
    \end{tabu}
    \caption{Usign tabu rowcolors to draw white and red huge lines}
    \label{tab:tab1}
\end{table}

\vspace{-\baselineskip}

\begin{table}[!h]
    \def\TableLineSize{0.75pt}
    \small  
    \tabulinesep = 1.5pt 
    \tabulinestyle{\TableLineSize white}
    \taburulecolor|red|{white}
    \arrayrulewidth=2pt \doublerulesep=2pt
    \taburowcolors[2] 2{gray!25 .. gray!50}
    \begin{tabu} to \linewidth {X[1.2,mc]|X[1,mc]|X[2.2,mc]} \firsthline\hline
        \rowcolor{gray!60}
        Something & Number & \makecell{Something \\(splitted)} \\ \firsthline\hline
        \cellcolor{gray}& 1 & Here is a sentence automatically breaked  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \multirow{-10}{*}{\cellcolor{gray}Multirow}
        & 1 & 2 secs  \\ \firsthline\hline
    \end{tabu}
    \caption{Idem as table 1 but with thin separating lines}
    \label{tab:tab1}
\end{table}

\lipsum[4]
\lipsum[4]
some text
\vspace{1.5\baselineskip}
\begin{table}[!h]
    \def\TableLineSize{1.5pt}
    \small
    \tabulinesep =1.5pt 
    \tabulinestyle{\TableLineSize red}
    \taburowcolors[2] 2{gray!25 .. gray!50}
    \begin{tabu} to \linewidth {X[1.2mc]X[1,mc]X[2.2,mc]} \tabucline -
        \rowcolor{gray!60}
        Something & Number & \makecell{Something \\(splitted)}\\ \tabucline -
        \cellcolor{gray}& 1 & Here is a sentence automatically breaked  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \cellcolor{gray}& 1 & 2 secs  \\
        \multirow{-10}{*}{\cellcolor{gray}Multirow}
        & 1 & 2 secs  \\ \tabucline -
    \end{tabu}
    \caption{Normal 1.5pt red lines}
    \label{tab:tab1}
\end{table}

\vspace{1.5\baselineskip}
\begin{table}[!h]
    \def\TableLineSize{0.75pt}
    \small
    \tabulinesep = 1.5pt
    \tabulinestyle{\TableLineSize white}
    \arrayrulewidth=1.5pt
    \taburowcolors[2] 2{gray!25 .. gray!50}
    \begin{tabu} to \linewidth {X[1.2,mc]|X[1,mc]|X[2.2,mc]} \tabucline[red] -
        \rowcolor{gray!60}
        Something & Number & \makecell{Something \\(splitted)} \\ \tabucline[red] -
        \cellcolor{gray}& 1 & Here is a sentence automatically breaked  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \cellcolor{gray}& 1 & 2 secs  \\[-\TableLineSize] \tabucline {2-3}
        \multirow{-10}{*}{\cellcolor{gray}Multirow}
        & 1 & 2 secs  \\ \tabucline[red] -
    \end{tabu}
    \caption{Idem as table 3 but with thin separating lines}
    \label{tab:tab1}
\end{table}

\lipsum[1-2] 

\end{document}

答案3

供参考,以下是可以用 做{NiceTabular}什么nicematrix

\documentclass{article}
\usepackage{booktabs,nicematrix,tikz}

\begin{document}

\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\begin{NiceTabular}{M{2cm}M{3cm}M{4cm}}%
   [ 
     code-before = { 
                     \rowcolor{gray!60}{1}
                     \rowcolors{2}{gray!20}{gray!40}
                     \columncolor{gray!60}{1} 
                     \tikz [line width = 0.1pt, white]
                     \draw (1-|2) -- (2-|2)(1-|3) -- (8-|3) ;
                   }
   ]
\arrayrulecolor{red}\toprule
Something & Number & \begin{tabular}{c}Something \\(splitted)\end{tabular}\\\midrule
\Block{6-1}{Multirow} & 1 & Here is a sentence automatically breaked\\
 & 2 & 2 secs \\
 & 3 & 2 secs \\
 & 4 & 2 secs \\
 & 5 & 2 secs \\
 & 6 & 2 secs \\ \bottomrule 
\end{NiceTabular}
\end{document}

上述代码的结果

相关内容