12

I have a simple booktabs table very similar to the following MWE:

\documentclass[12pt]{article}

% set the font of the document
\usepackage{fontspec}

% 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}

\begin{document}

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

\end{document}

Which produces output like this:

enter image description here

As can be seen, the row color fill doesn't fill the height of the row, how can this be fixed?

Edit:

In addition is it possible to fix this issue when coloring as single cell such as in the following MWE:

\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}

\begin{document}

\begin{center}
\begin{tabular}{ccc}
\toprule 
Variable &  Field One &  Field Two \\
\midrule
\cellcolor{my-blue}{A} & 0.0002 & 0.0005 \\
\bottomrule
\end{tabular}
\end{center}

\end{document}

enter image description here

To fill the height of the cell?

Ben
  • 1,229

3 Answers3

12

The space below and above a rule can be filled by a colored rule:

\documentclass[12pt]{article}

% set the font of the document
%\usepackage{fontspec}

% 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{my-blue}
\rowcolor{my-blue} Variable &  Field One &  Field Two \\
\aboverulesepcolor{my-blue}
\midrule
A & 0.0002 & 0.0005 \\
\bottomrule
\end{tabular}
\end{center}

\end{document}

Result

Heiko Oberdiek
  • 271,626
2

The environment {NiceTabular} of nicematrix provides tools to color cells, rows and columns in a way compatible with the horizontal rules of booktabs. Moreover, you won't see thin white lines, whatever PDF viewer you use.

\documentclass[12pt]{article}

\usepackage{setspace} \setstretch{1.15} \usepackage{relsize}

\usepackage{booktabs} \usepackage{nicematrix} \usepackage{xcolor}

\begin{document}

\begin{center} \begin{NiceTabular}{ccc}[colortbl-like] \toprule \rowcolor{blue!15} Variable & Field One & Field Two \ \midrule A & 0.0002 & 0.0005 \ \bottomrule \end{NiceTabular} \end{center}

\end{document}

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

Output of the above code

F. Pantigny
  • 40,250
0

I've used the code of Heiko and had a problem with the usage in the longtable environment. This was my solution. And it works well with the tabular environment too. Hence you should define new lines instead of adding a colored rule:

\colorlet{tableheadcolor}{gray!35} % Table header colour = 25% gray
\newcommand{\topline}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\midline}{\arrayrulecolor{tableheadcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
Dorian
  • 35
  • 6