5

As stated in the headline, I am trying to add color to my tables but it seems the booktab package leave extra white space between \toprule and \bottomrule that are not covered by \xcolor picture

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\renewcommand{\arraystretch}{1.5}
\rowcolors{1}{green}{blue}
\begin{tabular}{ll}
\toprule
foo&foo\\
bar&bar\\
foo&bar\\
\bottomrule
\end{tabular}
\end{document}

The problem does not occur when I used \hline instead. I would like to know if there is an easy fix and if it is not too much trouble, an explanation of what is going on here?Thanks in advance.

3

2 Answers 2

5

The space is left over by the toprule and bottom rule-- which space can be separately colred by the command \aboverulesepcolor and \belowrulesepcolor set to the same color as the \rowcolor enter image description here

\documentclass[12pt]{article} 

% set line spacing 
\usepackage{setspace} 
\setstretch{1.15} 
\usepackage{relsize} 

% table materials 
\usepackage{tabularx} 
\usepackage{booktabs} 
\usepackage{array} 
\usepackage{multirow} 

% define colours 
\usepackage{xcolor, colortbl} 
\definecolor{my-blue}{RGB}{83,87,118} 

\usepackage{microtype} 

\newcommand*{\belowrulesepcolor}[1]{% 
  \noalign{% 
    \kern-\belowrulesep 
    \begingroup 
      \color{#1}% 
      \hrule height\belowrulesep 
    \endgroup 
  }%
} 
\newcommand*{\aboverulesepcolor}[1]{% 
  \noalign{% 
    \begingroup 
      \color{#1}% 
      \hrule height\aboverulesep 
    \endgroup 
    \kern-\aboverulesep 
  }%
} 

\begin{document} 

\begin{center} 
\begin{tabular}{ccc} 
\toprule 
\belowrulesepcolor{green} 
\rowcolor{green} Variable &  Field One &  Field Two \\ 
\aboverulesepcolor{green} 
\midrule 
\belowrulesepcolor{blue} 
\rowcolor{blue}A & 0.0002 & 0.0005 \\ 
\aboverulesepcolor{blue}
\bottomrule  
\end{tabular} 
\end{center} 

\end{document}
2
  • The colors appear to bleed beyond the width of the table when using longtable. Any solution so works with longtable?
    – cpage
    Mar 28, 2021 at 20:26
  • please ask a new question to attract a larger audience
    – js bibra
    Mar 29, 2021 at 0:00
5

With {NiceTabular} of nicematrix, you have directly (after two compilations) the result you wish:

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

\begin{document}
\renewcommand{\arraystretch}{1.5}
\begin{NiceTabular}{ll}
\CodeBefore 
   \rowcolors{1}{green!15}{blue!15}
\Body
\toprule
foo&foo\\
bar&bar\\
foo&bar\\
\bottomrule
\end{NiceTabular}    
\end{document}

Result of the above code

0

You must log in to answer this question.

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