用颜色绘制表格

用颜色绘制表格

我正在尝试在我的 tex 文档中创建一个表格,该表格或多或少类似于下图中的表格:

在此处输入图片描述

我找到了这段代码,但我仍然不知道如何分离图片中显示的每个表。

 \documentclass{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{graphicx}% http://ctan.org/pkg/graphicx

\colorlet{tableheadcolor}{gray!25} % Table header colour = 25% gray
\newcommand{\headcol}{\rowcolor{tableheadcolor}} %
\colorlet{tablerowcolor}{gray!10} % Table row separator colour = 10% gray
\newcommand{\rowcol}{\rowcolor{tablerowcolor}} %
    % Command \topline consists of a (slightly modified) \toprule followed by a \heavyrule rule of colour tableheadcolor (hence, 2 separate rules)
\newcommand{\topline}{\arrayrulecolor{black}\specialrule{0.1em}{\abovetopsep}{0pt}%
            \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
    % Command \midline consists of 3 rules (top colour tableheadcolor, middle colour black, bottom colour white)
\newcommand{\midline}{\arrayrulecolor{tableheadcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
    % Command \rowmidlinecw consists of 3 rules (top colour tablerowcolor, middle colour black, bottom colour white)
\newcommand{\rowmidlinecw}{\arrayrulecolor{tablerowcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
    % Command \rowmidlinewc consists of 3 rules (top colour white, middle colour black, bottom colour tablerowcolor)
\newcommand{\rowmidlinewc}{\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{tablerowcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
    % Command \rowmidlinew consists of 1 white rule
\newcommand{\rowmidlinew}{\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
    % Command \rowmidlinec consists of 1 tablerowcolor rule
\newcommand{\rowmidlinec}{\arrayrulecolor{tablerowcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
    % Command \bottomline consists of 2 rules (top colour
\newcommand{\bottomline}{\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{\belowbottomsep}}%
\newcommand{\bottomlinec}{\arrayrulecolor{tablerowcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{\belowbottomsep}}%

\begin{document}

\renewcommand{\arraystretch}{1.5}
\begin{tabular}{llll}
  \topline
  \headcol Approach & Estimator & ME & MAE \\
  \midline
  & $\widehat{JV}_{\text{na\"ive}}$ & 4.37e-07 & 4.37e-07 \\
  \rowcol \smash{\raisebox{1em}{Na\"ive}} & $\widetilde{JV}_{\text{na\"ive}}$ & 3.88e-07 & 3.88e-07 \\
  & $\widehat{JV}$ & 1.33e-06 & 1.33e-06 \\
   \rowcol \smash{\raisebox{1em}{Regular}} & $\widetilde{JV}$ & 1.20e-06 & 1.20e-06 \\
  \bottomlinec
\end{tabular}

\end{document}

答案1

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\usepackage{graphicx}
\newcommand\SR{\specialrule{1pt}{1pt}{1pt}}

\begin{document}

\begingroup
\arrayrulecolor{white}\arrayrulewidth=1pt
\begin{tabular}[t]{c !{\color{white}\vrule width3pt} c !{\color{white}\vrule width3pt} c }
\rowcolor{black}\textcolor{white}{Movies} &\textcolor{white}{Ratings}\\\SR
\rowcolor{gray!25}ID & Movie ID\\\SR
\rowcolor{gray!10}Title (Year) & User ID\\\SR
\rowcolor{gray!25}Genre List & Rating \\\SR
                             & \cellcolor{gray!10}Timestamp
\end{tabular}
\endgroup

\end{document}

在此处输入图片描述

答案2

这可能是一个不受欢迎的观点,但在表格中使用交替颜色的最佳方式是不要使用它们。交替行颜色的趋势可以追溯到 Word,它曾一度将其用作默认格式。然而,这并不意味着这是好的做法。由于背景颜色的变化,字体和背景之间的对比度会降低,这实际上会使阅读更加困难。我建议改用干净、专业的设计:

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{tabular}{@{}lll@{}}
\toprule
Movies & Ratings & Tags\\
\midrule
ID & Movie ID & User ID\\
Title (Year) & User ID & Movie ID\\
Genre List & Rating & Tag\\
& Timestamp & Timestamp\\
\bottomrule
\end{tabular}


\end{document}

相关内容