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?

3 Answers 3

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

4
  • 1
    Thank you this works. I'm also interested in just colouring one cell using \cellcolor{} how would I go about applying this solution to the same problem created using that command? As this applies the color to the whole row and not just a single cell.
    – Ben
    Sep 8, 2015 at 11:31
  • @Ben There is no easy way, because the width and position of the cell is usually not known. Sep 8, 2015 at 11:40
  • Is there another way to colour the background of a single cell properly then?
    – Ben
    Sep 8, 2015 at 12:09
  • 1
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

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

You must log in to answer this question.

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