5

I would like to highlight the first two rows of my table with a custom shade of gray. For this, I thought of using the \rowcolor environment from the colortbl package.

However, its usage seems to leave unwanted gaps in the shading, i.e. the shading patch does not get applied to the full row.

Here is a MWE

\documentclass{article}
\usepackage{xcolor}
\definecolor{newgray}{RGB}{196,196,196}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{threeparttable}

\begin{document}
\begin{threeparttable}
    \centering
    \begin{tabular*}{\textwidth}{ l @{\extracolsep{\fill}} l c r}
        \multicolumn{4}{c}{\textbf{Random Table}} \\
        \toprule
        \multicolumn{1}{l}{Item} & \multicolumn{1}{l}{Case 1} & \multicolumn{1}{c}{Case 2} & \multicolumn{1}{r}{Case 3}\\
        \midrule

        \rowcolor{newgray}first thing, & \tnote{a}245  & \tnote{b}25e-6  & \tnote{c}79e-6 \\
        \rowcolor{newgray}[20pt][20pt]second thing & \tnote{a}450  & ---             & \tnote{b}24e-6 \\
        third thing                    & \tnote{d}2e-6 & \tnote{c}4123.5 & \tnote{b}2e-6  \\
        fourth thing                   & ---           & 1e6             & no data        \\
        \bottomrule
    \end{tabular*}
    \medskip
    \begin{tablenotes}
    \item[a] some explanation
    \item[b] this is why \dots
    \item[c] see reference awesome
    \item[d] hello world
    \item
    \end{tablenotes}
\end{threeparttable}
\end{document}

which produces the following output

rowcolor not uniformly shading the row

Here are the requirements. I would like to have the table typeset to the textwidth achieved here using \begin{tabular*}{\textwidth}{ l @{\extracolsep{\fill}} l c r}, and secondly I need to have explanatory notes achieved here using the tablenotes environment provided by the threeparttable package. For aesthetics, I am using the booktabs package.

Clearly, the shading achieved right now is not desirable. When used without any overhang specification, the \rowcolor command leaves gaps in the shading. While, using left and right overhang minimises these shading gaps in the middle columns, it extends the shading beyond the table's width, which is undesirable.

How can I achieve the expected behavior, i.e. shade the row uniformly, with no gaps and without exceeding table's width?

2 Answers2

4
  • @{\extracolsep{\fill}} introduce additional space between columns which is not covered by rowcolor
  • in second line you have \rowcolor{newgray}[20pt][20pt] which should be only \rowcolor{newgray}
  • coloring rows and use rules from booktabs doesn't gives nice result, instead them i suggest lines provided by package boldline
  • for more vertical space in cells i suggest to use calspace

enter image description here

\documentclass{article}
\usepackage[table]{xcolor} % it call and enhance colortbl
\definecolor{newgray}{RGB}{196,196,196}
\usepackage{booktabs, threeparttable}
\usepackage{tabularx}       % new
\usepackage{boldline}       % new (part of shipunov bundle)
\usepackage{cellspace}      % new
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\addparagraphcolumntypes{X}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}
\begin{threeparttable}
    \centering
\begin{tabularx}{\textwidth}{ S{>{\hsize=0.4\hsize}L}
                                >{\hsize=0.2\hsize}L
                                >{\hsize=0.2\hsize}C
                                >{\hsize=0.2\hsize}R}
\multicolumn{4}{c}{\textbf{Random Table}}                               \\
    \hlineB{2}
        Item    & Case 1        &   Case 2          &   Case 3          \\
    \hlineB{1.75}
    \rowcolor{newgray}
first thing,    & \tnote{a}245  & \tnote{b}25e-6    & \tnote{c}79e-6    \\
    \rowcolor{newgray}
second thing    & \tnote{a}450  & ---               & \tnote{b}24e-6    \\
third thing     & \tnote{d}2e-6 & \tnote{c}4123.5   & \tnote{b}2e-6     \\
fourth thing    & ---           & 1e6               & no data           \\
    \hlineB{2}
    \end{tabularx}

    \begin{tablenotes}
    \item[a] some explanation
    \item[b] this is why \dots
    \item[c] see reference awesome
    \item[d] hello world
    \item
    \end{tablenotes}
\end{threeparttable}
\end{document}

edit: if you like to have simpler solution with less nice table (which width is still equal to text width), than try the following:

\documentclass{article}
\usepackage[table]{xcolor}  % it call and enhance colortbl
\definecolor{newgray}{RGB}{196,196,196}
\usepackage{booktabs, threeparttable}
\usepackage{tabularx}       % new
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}
\begin{threeparttable}
    \centering
    \renewcommand\arraystretch{1.2}
\begin{tabularx}{\textwidth}{ L L C R }
\multicolumn{4}{c}{\textbf{Random Table}}                               \\
    \hline
        Item    & Case 1        &   Case 2          &   Case 3          \\
    \hline
    \rowcolor{newgray}
first thing,    & \tnote{a}245  & \tnote{b}25e-6    & \tnote{c}79e-6    \\
    \rowcolor{newgray}
second thing    & \tnote{a}450  & ---               & \tnote{b}24e-6    \\
third thing     & \tnote{d}2e-6 & \tnote{c}4123.5   & \tnote{b}2e-6     \\
fourth thing    & ---           & 1e6               & no data           \\
    \hline
\end{tabularx}

\begin{tablenotes}
\item[a] some explanation
\item[b] this is why \dots
\item[c] see reference awesome
\item[d] hello world
\item
\end{tablenotes}
\end{threeparttable}
\end{document}

note:

  • S{X} specifier means that to selected X column is added vertical space defined by

    \setlength\cellspacetoplimit{4pt}
    \setlength\cellspacebottomlimit{4pt}
    
  • with >{\hsize=0.4\hsize} and >{\hsize=0.2\hsize} is changed ratio between columns widths (first column has width equal 0.4\textwidth, other three have width equal 0.2\textwidth each)

  • the first solution can be simplified by removing cellspace stuff and for more vertical spaces around cells contents use simpler \renewcommand\arraystretch{1.2}:

    \documentclass{article}
    \usepackage[table]{xcolor} % it call and enhance colortbl
    \definecolor{newgray}{RGB}{196,196,196}
    \usepackage{booktabs, threeparttable}
    \usepackage{tabularx}       % new
    \usepackage{boldline}       % new (part of shipunov bundle)
    \newcolumntype{C}{>{\centering\arraybackslash}X}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
    
    \begin{document}
    \begin{threeparttable}
        \centering
        \renewcommand\arraystretch{1.2}
    \begin{tabularx}{\textwidth}{>{\hsize=0.4\hsize}L
                                 >{\hsize=0.2\hsize}L
                                 >{\hsize=0.2\hsize}C
                                 >{\hsize=0.2\hsize}R}
    
     %% table body and table notes %%
    
    \end{threeparttable}
    \end{document}
    
Zarko
  • 296,517
1

The environments {NiceTabular} and {NiceTabular*} of nicematrix provide tools to color cells, rows and columns without the drawbacks of colortbl.

Here, you have directly the expected result (but you need several commpilations).

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{nicematrix}

\begin{document} \begin{threeparttable} \centering \begin{NiceTabular}{\textwidth}{ l @{\extracolsep{\fill}} l c r}% [code-before = \rowcolor[RGB]{196,196,196}{3,4}] \multicolumn{4}{c}{\textbf{Random Table}} \ \toprule \multicolumn{1}{l}{Item} & \multicolumn{1}{l}{Case 1} & \multicolumn{1}{c}{Case 2} & \multicolumn{1}{r}{Case 3}\ \midrule first thing, & \tnote{a}245 & \tnote{b}25e-6 & \tnote{c}79e-6 \ second thing & \tnote{a}450 & --- & \tnote{b}24e-6 \ third thing & \tnote{d}2e-6 & \tnote{c}4123.5 & \tnote{b}2e-6 \ fourth thing & --- & 1e6 & no data \ \bottomrule \end{NiceTabular} \medskip \begin{tablenotes} \item[a] some explanation \item[b] this is why \dots \item[c] see reference awesome \item[d] hello world \item \end{tablenotes} \end{threeparttable} \end{document}

Ouput of the above code

F. Pantigny
  • 40,250