10

A three line table is used for my report. I want to fill lightgray background color to the whole table, but there is white space between rows, how to eliminate this issue?

  1. Without \addlinespace: there is still white space around rules enter image description here

  2. With \addlinespace: this is ideal spacing between rows, but how to fill colour as well for these white spaces? enter image description here


  \usepackage{colortbl}
  \definecolor{Lightgray}{RGB}{235,235,235}

Code:

  \begin{table} [htpb]
  \centering
  \begin{minipage}[b]{0.9 \textwidth}
  \centering
  \begin{threeparttable}[b]

  \caption{Size and shape distribution for a sampled rock riprap} 
  \label{tab:size_shape}
  \begin{tabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
  \toprule
  \rowcolor{Lightgray}& Small    &  Medium &  Large & Shape fraction \\        \cmidrule{1-5}  \addlinespace
  \rowcolor{Lightgray}Compact    & 10\% &  44\% & 7\% & 61\% \\ \addlinespace
  \rowcolor{Lightgray}Flat       & 4\%  &  10\% & 4\% & 18\% \\ \addlinespace       
  \rowcolor{Lightgray}Elongated  & 5\%  &  12\% & 4\% & 21\% \\ \addlinespace
  \midrule
  \rowcolor{Lightgray}Size fraction  & 19\% & 66\% & 15\% & 100\% \\
  \bottomrule
  \end{tabular}
  \end{threeparttable}
  \end{minipage}
  \end{table} 

Edit 1: Using \renewcommand{\arraystretch}{1.3} gets proper spacing between rows without \addlinespace. The remaining issue is: how to get ride of the whihte space for rules?

Edit 2: The solution of Professional-looking tables with alternating row colors still could not eliminate white space after midline or cmidrule. Hence white colour is used for the row after midline to hide the white space. If wrong please correct me.

See the table created by the new commands of that solution: enter image description here

   \begin{table} [htpb]
   \centering
   \begin{minipage}[b]{0.9 \textwidth}
   \centering
   \begin{threeparttable}[b]

   \caption{Size and shape distribution for a sampled rock riprap} 
   \label{tab:size_shape}
   \begin{tabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
   \topline
   \headcol & Small  &  Medium &  Large & Shape fraction \\% \cmidrule{1-5}  
   \midline
   \rowcol Compact    & 10\% &  44\% & 7\% & 61\% \\ 
   \rowcol Flat       & 4\%  &  10\% & 4\% & 18\% \\ % \cellcolor{lightgray}  
   \rowcol Elongated  & 5\%  &  12\% & 4\% & 21\% \\ 
   \midline
   \rowcol Size fraction  & 19\% & 66\% & 15\% & 100\% \\
   \bottomlinec  %\bottomrule
   \end{tabular}
   \end{threeparttable}
   \end{minipage}
   \end{table}

Besides, if you enlarge the table and look carefully, you will see few thin white lines in the heading row! Is that normal?

See below: lines

4
  • See Werner's answer to tex.stackexchange.com/q/33758. Jan 12, 2013 at 19:28
  • The answer codes of tex.stackexchange.com/q/33758 seems not working.
    – KOF
    Jan 12, 2013 at 19:34
  • Can you please add to your question a complete document with the code you used to produce the image? (I mean the one containing the commands from the answer I linked to in my previous comment). Jan 12, 2013 at 20:15
  • Please look to the final commands to produce the table below.
    – KOF
    Jan 12, 2013 at 21:02

2 Answers 2

12

After looking into the new commands provided by Werner, it seems that the mideline is redefined by 3 rules with top colour, middle colour and bottom colour.

So two new commands were added to make a perfect colour filled table:

  1. Middle line connecting heading row and the second row

    \colorlet{blcolor}{gray!80}
    
    \newcommand{\rowmidlineHR}{\arrayrulecolor{tableheadcolor}
      \specialrule{\aboverulesep}{0pt}{0pt}%
      \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
      \arrayrulecolor{tablerowcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
      \arrayrulecolor{black}}
      % Command \rowmidlinewc consists of 3 rules
      % (top colour tableheadcolor, middle colour black, bottom colour tablerowcolor)
    
  2. Secondary gray middle line

    \newcommand{\rowmidlineG}{\arrayrulecolor{tablerowcolor}%
      \specialrule{\aboverulesep}{0pt}{0pt}%
      \arrayrulecolor{blcolor}\specialrule{\lightrulewidth}{0pt}{0pt}%
      \arrayrulecolor{tablerowcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
      \arrayrulecolor{black}}
    % Command \rowmidlineG consists of 3 rules
    % (top colour tablerowcolor, middle colour black, bottom colour tablerowcolor)
    

Final table: perfect table

and the codes:

\renewcommand{\arraystretch}{1.3}

\begin{table} [htpb]
\centering
\begin{minipage}[b]{0.9 \textwidth}
\centering
\begin{threeparttable}[b]

\caption{Size and shape distribution for a sampled rock riprap} 
\label{tab:size_shape}
\begin{tabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
\topline
\headcol & Small  &  Medium &  Large & Shape fraction \\
\rowmidlineHR  % Head Rule
\rowcol Compact    & 10\% &  44\% & 7\% & 61\% \\ 
\rowcol Flat       & 4\%  &  10\% & 4\% & 18\% \\ 
\rowcol Elongated  & 5\%  &  12\% & 4\% & 21\% \\ 
\rowmidlineG % Gray midline
\rowcol Size fraction  & 19\% & 66\% & 15\% & 100\% \\
\bottomlinec
\end{tabular}
\end{threeparttable}
\end{minipage}
\end{table}
1

With {NiceTabular} of nicematrix, you have a command to color the whole tabular. You have directly the expected result.

However, in order to use {NiceTabular} in {threeparttable}, you have first to tell threeparttable that it should take into account the environment {NiceTabular} (with the dedicated hook of threeparttable):

\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother

For the whole MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{threeparttable}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage[left=2.5cm,right=2.5cm]{geometry}

\begin{document}

\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother


\begin{table} [htpb]
\centering
\begin{minipage}[b]{0.9 \textwidth}
\centering
\begin{threeparttable}[b]

\caption{Size and shape distribution for a sampled rock riprap} 
\label{tab:size_shape}
\begin{NiceTabular} { p{0.15 \textwidth}  p{0.10 \textwidth} p{0.10 \textwidth} p{0.10 \textwidth} p{0.15 \textwidth}  } 
\CodeBefore
  \arraycolor[gray]{0.9}
\Body
\toprule
& Small    &  Medium &  Large & Shape fraction \\ \cmidrule{1-5}  \addlinespace
Compact    & 10\% &  44\% & 7\% & 61\% \\ \addlinespace
Flat       & 4\%  &  10\% & 4\% & 18\% \\ \addlinespace       
Elongated  & 5\%  &  12\% & 4\% & 21\% \\ \addlinespace
\midrule
Size fraction  & 19\% & 66\% & 15\% & 100\% \\
\bottomrule
\end{NiceTabular}
\end{threeparttable}
\end{minipage}
\end{table} 

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .