2

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
  • @{\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}
    
  • it is not clear what the { S{>{\hsize=0.4\hsize}L} >{\hsize=0.2\hsize}L >{\hsize=0.2\hsize}C >{\hsize=0.2\hsize}R} commands are doing here. Can you provide a simpler example without using these commands ? – Krishna May 28 '18 at 21:09
  • 1
    it will become more clear after reading documentation for packages cellspace and tabularx :-). see edited answer. – Zarko May 28 '18 at 21:39
  • thank you very much for the edits clarifying the question. Much appreciate your efforts. – Krishna May 29 '18 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.