如何制作一个完美的彩色三部分表格?

如何制作一个完美的彩色三部分表格?

我的报告使用三行表格。我想lightgray为整个表格填充背景颜色,但是行与行之间有空白,如何消除此问题?

  1. 没有\addlinespace:规则周围仍有空白 在此处输入图片描述

  2. \addlinespace这是行之间的理想间距,但如何为这些白色空间填充颜色? 在此处输入图片描述


  \usepackage{colortbl}
  \definecolor{Lightgray}{RGB}{235,235,235}

代码:

  \begin{table} [htpb]
  \centering
  \begin{minipage}[b]{0.9 \textwidth}
  \centering
  \begin{threeparttable}[b]

  \caption{Size and shape distribution for a sampled rock riprap} 
  \label{tab:size_shape}
  \begin{tabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
  \toprule
  \rowcolor{Lightgray}& Small    &  Medium &  Large & Shape fraction \\        \cmidrule{1-5}  \addlinespace
  \rowcolor{Lightgray}Compact    & 10\% &  44\% & 7\% & 61\% \\ \addlinespace
  \rowcolor{Lightgray}Flat       & 4\%  &  10\% & 4\% & 18\% \\ \addlinespace       
  \rowcolor{Lightgray}Elongated  & 5\%  &  12\% & 4\% & 21\% \\ \addlinespace
  \midrule
  \rowcolor{Lightgray}Size fraction  & 19\% & 66\% & 15\% & 100\% \\
  \bottomrule
  \end{tabular}
  \end{threeparttable}
  \end{minipage}
  \end{table} 

编辑1: 使用\renewcommand{\arraystretch}{1.3}可以获得适当的行间距,而无需\addlinespace。剩下的问题是:如何摆脱规则的空白?

编辑2:解决方案具有交替行颜色的专业外观表格仍然无法消除midline或之后的空白cmidrule。因此,空白后的行使用颜色。如果错误,请纠正我whitemidlinehide

查看该解决方案的新命令创建的表: 在此处输入图片描述

   \begin{table} [htpb]
   \centering
   \begin{minipage}[b]{0.9 \textwidth}
   \centering
   \begin{threeparttable}[b]

   \caption{Size and shape distribution for a sampled rock riprap} 
   \label{tab:size_shape}
   \begin{tabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
   \topline
   \headcol & Small  &  Medium &  Large & Shape fraction \\% \cmidrule{1-5}  
   \midline
   \rowcol Compact    & 10\% &  44\% & 7\% & 61\% \\ 
   \rowcol Flat       & 4\%  &  10\% & 4\% & 18\% \\ % \cellcolor{lightgray}  
   \rowcol Elongated  & 5\%  &  12\% & 4\% & 21\% \\ 
   \midline
   \rowcol Size fraction  & 19\% & 66\% & 15\% & 100\% \\
   \bottomlinec  %\bottomrule
   \end{tabular}
   \end{threeparttable}
   \end{minipage}
   \end{table}

此外,如果你放大表格并仔细观察,你会发现标题行中有几条细细的白线!这是正常的吗?

见下文: 线

答案1

在研究了沃纳,似乎是mideline由顶部颜色、中间颜色和底部颜色3条规则重新定义的。

因此添加了两个新命令来制作完美填充颜色的表格:

  1. 连接标题行和第二行的中间线

    \colorlet{blcolor}{gray!80}
    
    \newcommand{\rowmidlineHR}{\arrayrulecolor{tableheadcolor}
      \specialrule{\aboverulesep}{0pt}{0pt}%
      \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
      \arrayrulecolor{tablerowcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
      \arrayrulecolor{black}}
      % Command \rowmidlinewc consists of 3 rules
      % (top colour tableheadcolor, middle colour black, bottom colour tablerowcolor)
    
  2. 次要灰色中线

    \newcommand{\rowmidlineG}{\arrayrulecolor{tablerowcolor}%
      \specialrule{\aboverulesep}{0pt}{0pt}%
      \arrayrulecolor{blcolor}\specialrule{\lightrulewidth}{0pt}{0pt}%
      \arrayrulecolor{tablerowcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
      \arrayrulecolor{black}}
    % Command \rowmidlineG consists of 3 rules
    % (top colour tablerowcolor, middle colour black, bottom colour tablerowcolor)
    

决赛桌: 完美的餐桌

以及代码:

\renewcommand{\arraystretch}{1.3}

\begin{table} [htpb]
\centering
\begin{minipage}[b]{0.9 \textwidth}
\centering
\begin{threeparttable}[b]

\caption{Size and shape distribution for a sampled rock riprap} 
\label{tab:size_shape}
\begin{tabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
\topline
\headcol & Small  &  Medium &  Large & Shape fraction \\
\rowmidlineHR  % Head Rule
\rowcol Compact    & 10\% &  44\% & 7\% & 61\% \\ 
\rowcol Flat       & 4\%  &  10\% & 4\% & 18\% \\ 
\rowcol Elongated  & 5\%  &  12\% & 4\% & 21\% \\ 
\rowmidlineG % Gray midline
\rowcol Size fraction  & 19\% & 66\% & 15\% & 100\% \\
\bottomlinec
\end{tabular}
\end{threeparttable}
\end{minipage}
\end{table}

答案2

使用{NiceTabular}nicematrix您可以使用命令为整个表格着色。您可以直接获得预期的结果。

然而,为了{NiceTabular}在 中使用{threeparttable},您必须首先告诉threeparttable它应该考虑环境{NiceTabular}(使用 的专用挂钩threeparttable):

\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother

对于整个 MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{threeparttable}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage[left=2.5cm,right=2.5cm]{geometry}

\begin{document}

\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother


\begin{table} [htpb]
\centering
\begin{minipage}[b]{0.9 \textwidth}
\centering
\begin{threeparttable}[b]

\caption{Size and shape distribution for a sampled rock riprap} 
\label{tab:size_shape}
\begin{NiceTabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
\CodeBefore
  \arraycolor[gray]{0.9}
\Body
\toprule
& Small    &  Medium &  Large & Shape fraction \\ \cmidrule{1-5}  \addlinespace
Compact    & 10\% &  44\% & 7\% & 61\% \\ \addlinespace
Flat       & 4\%  &  10\% & 4\% & 18\% \\ \addlinespace       
Elongated  & 5\%  &  12\% & 4\% & 21\% \\ \addlinespace
\midrule
Size fraction  & 19\% & 66\% & 15\% & 100\% \\
\bottomrule
\end{NiceTabular}
\end{threeparttable}
\end{minipage}
\end{table} 

\end{document}

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

上述代码的输出

相关内容