2

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

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}
| improve this answer | |
1

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}[code-before = \rowcolors{1}{green!15}{blue!15}]
\toprule
foo&foo\\
bar&bar\\
foo&bar\\
\bottomrule
\end{NiceTabular}    
\end{document}

Result of the above code

| improve this answer | |
  • Since version 5.0 of nicematrix one must write ll instead of LL in the preamble of {NiceTabular} (but there is an option for backward compatibility). – F. Pantigny Jul 18 at 20:34

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.