Different Colors in different rows in table

These kind of issues are mostly related to the PDF viewer, they will not appear in a print version of the document. One possible work-around, however, is to use just one row and use a tabular for the two-line cells. A new command \ccell can be quite handy for this.

\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}

enter image description here


A solution with makecell. I also improved the caption spacing w.r.t. to table (caption package) and added some vertical padding at the top and bottom of cells (cellspace package):

\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} 

enter image description here With the default vertical alignment of \makecell (centred), you obtain this, which looks nicer, in my opinion: enter image description here


I propose an alternate solution, following this one, which makes no use of vertical separators. I had to reduce by 1pt the vertical separation among columns and also the horizontal separation among the colored rows.

I used booktabs rules, and I used \addlinespace[0pt] to put the toprule exactly on the grey cells, without separation.

\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}

with this result:

enter image description here

Also, I feel you could totally avoid the top rule:

\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}

enter image description here

Tags:

Tables