制作表格时的背景和文字颜色

制作表格时的背景和文字颜色

我想让我的表格的背景颜色与该表格相似,但不知道如何将文本更改为白色并将顶列和行的背景更改为黑色。 在此处输入图片描述

到目前为止我已经使用了以下代码:

  \documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{multirow, booktabs}
\usepackage[dvipsnames,table]{xcolor}

\begin{document}

\begin{tabular}{|l c c c c c c c|}
\hline
\rowcolor{SeaGreen}\multicolumn{4}{|c}{\textbf{ESS3}} & \multicolumn{4}{c|}{\textbf{ESS5}} \\
\rowcolor{SeaGreen}
    \textbf{Utfall} & \textbf{Kontroll} & \textbf{Behandling} & \textbf{Diff} & \textbf{Kontroll} &  \textbf{Behandling} & \textbf{Diff}&  \textbf{DiD}            \\

\hline
\multicolumn{8}{|l|}{\textbf{Panel A: No controls}}\\
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ [1ex]

\multicolumn{8}{|l|}{\textbf{Panel B: Controlling for individual characteristics}}\\
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ [1ex]

\multicolumn{8}{|l|}{\textbf{Panel C: Controlling for individual characteristics}}\\
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ [1ex]

\multicolumn{8}{|l|}{\textbf{Panel D: Controlling for individual characteristics}}\\ [1ex]
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ [1ex]
\hline
\end{tabular}

输出结果如下:

在此处输入图片描述

所以我希望表格的布局与我已经制作的表格相同,但我不知道如何更改背景颜色和文本颜色。我还想删除表格顶部的白线,但不确定该怎么做。任何帮助都将不胜感激。

答案1

\colorbox这是一个使用和 的简单解决方案 \rowcolor

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{multirow, booktabs}
\usepackage[dvipsnames,svgnames, table]{xcolor}
\newcommand{\textbw}[1]{\bfseries\color{white}#1}
\setlength{\extrarowheight}{2pt}

\begin{document}

{\setlength{\fboxsep}{0pt}
    \colorbox{LightSteelBlue}{%
    \begin{tabular}{|l*{7}{c}|}
    \hline
    \rowcolor{DarkSlateGrey}\multicolumn{4}{c|}{\textbw{ESS3}} & \multicolumn{4}{c|}{\textbw{ESS5}} \\
    \rowcolor{DarkSlateGrey} \textbw{Utfall} & \textbw{Kontroll} & \textbw{Behandling} & \textbw{Diff} & \textbw{Kontroll} & \textbw{Behandling} & \textbw{Diff}& \textbw{DiD} \\

    \hline
    \multicolumn{8}{|l|}{\textbf{Panel A: No controls}}\\
    \textbf{Coef} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\
    \textbf{t-statistics} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\ [1ex]

    \multicolumn{8}{|l|}{\textbf{Panel B: Controlling for individual characteristics}}\\
    \textbf{Coef} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\
    \textbf{t-statistics} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\ [1ex]

    \multicolumn{8}{|l|}{\textbf{Panel C: Controlling for individual characteristics}}\\
    \textbf{Coef} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\
    \textbf{t-statistics} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\ [1ex]

    \multicolumn{8}{|l|}{\textbf{Panel D: Controlling for individual characteristics}}\\ [1ex]
    \textbf{Coef} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\
    \textbf{t-statistics} & 0.075 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 & 0.091 \\ [1ex]
    \hline
    \end{tabular}}%
}
 \end{document} 

在此处输入图片描述

答案2

以下是如何在黑色背景上实现白色标题的一种可能性。我也使用了包中的规则booktabs

使用颜色框使表格其余部分的背景变为蓝色的想法借鉴了伯纳德的回答

在此处输入图片描述

\documentclass{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{multirow, booktabs}
\usepackage[dvipsnames, svgnames, table]{xcolor}
\usepackage{tabularx}
\usepackage{caption}
\DeclareCaptionFormat{mycaptionformat}{%
  \colorbox{black}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2\color{white}\bfseries#3}}
} 
\captionsetup[table]{format=mycaptionformat,font={color=white,bf},skip=0pt}

\begin{document}

\begin{table}
\caption{my caption}
\setlength{\fboxsep}{0pt}
\colorbox{LightSteelBlue}{%
\begin{tabularx}{\textwidth}{X c c c c c c c}
\toprule
& \multicolumn{3}{c}{\textbf{ESS3}} & \multicolumn{3}{c}{\textbf{ESS5}} & \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
    \textbf{Utfall} & \textbf{Kontroll} & \textbf{Behandling} & \textbf{Diff} & \textbf{Kontroll} &  \textbf{Behandling} & \textbf{Diff}&  \textbf{DiD}            \\

\midrule
\multicolumn{8}{l}{\textbf{Panel A: No controls}}\\
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ \addlinespace

\multicolumn{8}{l}{\textbf{Panel B: Controlling for individual characteristics}}\\
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ \addlinespace

\multicolumn{8}{l}{\textbf{Panel C: Controlling for individual characteristics}}\\
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ \addlinespace

\multicolumn{8}{l}{\textbf{Panel D: Controlling for individual characteristics}}\\ [1ex]
\textbf{Coef}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\
\textbf{t-statistics}   &   0.075  &    0.091   &   0.091   &   0.091   &   0.091 &   0.091   &   0.091 \\ 
\bottomrule
\end{tabularx}}
\end{table}
\end{document}

相关内容