表格中不同行的不同颜色

表格中不同行的不同颜色

在此处输入图片描述

我怎样才能删除此表标题中出现的小白线?

我的代码是

    \documentclass{article}
\usepackage[table, dvipsnames]{xcolor}
\begin{document}
\begin{table}
        \centering
        \caption{Amount of exposure of default towards 3 counterparties}
        {\centering (with netting and collateral agreement)\par}
        \label{1}
        \begin{tabular}{|c | c| c|}
            \hline
            \rowcolor{lightgray}
            Counterparty & Amount of EAD & Amount of EAD\\
            \rowcolor{lightgray}
                         & (with netting) & (no netting)\\
            \hline  
            A & \$29,637,720 & \$92,604,560 \\
            B & \$38,250,400 & \$107,949,900\\
            C & \$124,619,600 & \$124,619,600 \\
            \hline
            Total&\$192,507,800 & \$325,174,100\\
            \hline          
        \end{tabular}
\end{table}
\end{document}

答案1

解决方案是makecell。我还改进了表格的标题间距(caption包),并在单元格的顶部和底部添加了一些垂直填充(cellspace包):

\documentclass{article}
\usepackage[table, svgnames, dvipsnames]{xcolor}
\usepackage{makecell, cellspace, caption}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}
\begin{document}

\begin{table}
        \centering\captionsetup{justification = centering}
        \caption{Amount of exposure of default towards 3 counterparties\\ (with netting and collateral agreement)}
        %{\centering}
        \label{1}
        \begin{tabular}{|Sc|Sc|Sc|}
            \hline
            \rowcolor{Gainsboro!60}
            Counterparty & \makecell{Amount of EAD\\ (with netting)} & \makecell{Amount of EAD\\ (no netting)}\\
            \hline
            A & \phantom{1}\$\,29,637,720 & \phantom{1}\$\,92,604,560 \\
            B & \phantom{1}\$\,38,250,400 & \$\,107,949,900\\
            C & \$\,124,619,600 & \$\,124,619,600 \\
            \hline
            Total&\$\,192,507,800 & \$\,325,174,100\\
            \hline
        \end{tabular}
\end{table}

\end{document} 

在此处输入图片描述 使用默认的垂直对齐方式\makecell(居中),您可以获得这样的效果,在我看来,这样看起来更美观: 在此处输入图片描述

答案2

这类问题主要与 PDF 查看器有关,它们不会出现在文档的打印版本中。不过,一种可能的解决方法是只使用一行,并使用 表示tabular两行单元格。新命令\ccell对此非常方便。

\documentclass{article}
\usepackage[table, dvipsnames]{xcolor}
\begin{document}

\newcommand{\ccell}[1]{\begin{tabular}[t]{@{}c@{}}#1\end{tabular}}

\begin{table}
    \centering
    \caption{Amount of exposure of default towards 3 counterparties}
    {\centering (with netting and collateral agreement)\par}
    \label{1}
    \begin{tabular}{|c|c|c|}
        \hline
        \rowcolor{lightgray}
        Counterparty & \ccell{Amount of EAD\\(with netting)} & \ccell{Amount of EAD\\(no netting)} \\ \hline
        A & \$29,637,720 & \$92,604,560      \\
        B & \$38,250,400 & \$107,949,900     \\
        C & \$124,619,600 & \$124,619,600    \\      \hline
        Total&\$192,507,800 & \$325,174,100  \\      \hline          
    \end{tabular}
\end{table}

\end{document}

在此处输入图片描述

答案3

我提出了一个替代解决方案,如下这个,它不使用垂直分隔符。我必须减少1pt列之间的垂直分隔符以及彩色行之间的水平分隔符。

我使用booktabs规则,并且将\addlinespace[0pt]顶部规则准确地放在灰色单元格上,不进行分离。

\documentclass{article}
\usepackage[table, dvipsnames]{xcolor}
\usepackage{array,booktabs}
\begin{document}
\begin{table}
        \centering
        \caption{Amount of exposure of default towards 3 counterparties}
        {\centering (with netting and collateral agreement)}
        \label{1}
        \begin{tabular}{c!{\vrule width -1pt}c!{\vrule width -1pt}c}
            \toprule\addlinespace[0pt]
            \rowcolor{lightgray}
            Counterparty & Amount of EAD & Amount of EAD\\[-1pt]
            \rowcolor{lightgray}
                         & (with netting) & (no netting)\\[2pt] 
            A & \$29,637,720 & \$92,604,560 \\
            B & \$38,250,400 & \$107,949,900\\
            C & \$124,619,600 & \$124,619,600 \\
            \midrule
            Total&\$192,507,800 & \$325,174,100\\
            \bottomrule          
        \end{tabular}
\end{table}
\end{document}

结果如下:

在此处输入图片描述

另外,我觉得你完全可以避免最重要的规则:

\documentclass{article}
\usepackage[table, dvipsnames]{xcolor}
\usepackage{array,booktabs}
\begin{document}
\begin{table}
        \centering
        \caption{Amount of exposure of default towards 3 counterparties}
        {\centering (with netting and collateral agreement)}
        \label{1}
        \begin{tabular}{c!{\vrule width -1pt}c!{\vrule width -1pt}c}
            \rowcolor{lightgray}
            Counterparty & Amount of EAD & Amount of EAD\\[-1pt]
            \rowcolor{lightgray}
                         & (with netting) & (no netting)\\[2pt] 
            A & \$29,637,720 & \$92,604,560 \\
            B & \$38,250,400 & \$107,949,900\\
            C & \$124,619,600 & \$124,619,600 \\
            \midrule
            Total&\$192,507,800 & \$325,174,100\\
            \bottomrule          
        \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案4

此外还有makecell和 (题外话)siunitxS列类型:

\documentclass{article}
\usepackage[table, svgnames, dvipsnames]{xcolor}
\usepackage{makecell}
\renewcommand\theadfont{\normalsize}
\usepackage{caption}
\usepackage{siunitx}

\begin{document}
    \begin{table}
    \centering
    \renewcommand\arraystretch{1.2}
    \captionsetup{justification = centering}
\caption{Amount of exposure of default towards 3 counterparties\\ (with netting and collateral agreement)}
    \label{1}
\begin{tabular}{|c|*{2}{>{\$\,}S[table-format=9.0,
                                 group-separator={,},
                                 table-align-text-pre=false,
                                 table-space-text-pre={\$\,}
                                 ]|}
                }
    \hline
\rowcolor{Gainsboro!60}
Counterparty 
    &   {\thead{Amount of EAD\\ (with netting)}} 
        &   {\thead{Amount of EAD\\ (no netting)}}   \\
    \hline
A   &    29 637 720 &    92 604 560                     \\
B   &    38 250 400 &   107 949 900                     \\
C   &   124 619 600 &   124 619 600                     \\
    \hline
Total
    &   192 507 800 &   325 174 100                     \\
    \hline
\end{tabular}
    \end{table}
\end{document} 

在此处输入图片描述

相关内容