# Fill between \toprule and \midrule

I've already tried one approach for this and had issues (Clear rowcolor at midrule) and now I've started having more. So instead of trying to fix that approach I thought it could be worth seeing what more experiences TeX-ers might suggest.

I'm trying to have it so that the row(s) between a \toprule and a \midrule in all my tables are filled with certain color, to create an effect like this:

Does anyone have any suggestions for how to go about this? For my current (deficient) approach see Clear rowcolor at midrule.

Specs: Using pdflatex with a bunch of packages including booktabe and xcolor (with the table option)

• Could you please add a complete but minimal example which reproduces your problem? – CarLaTeX Jun 9 at 5:48
• Since I'm asking for an approach, I don't see a MWE as being relevant. – tecosaur Jun 9 at 5:54
• Though if you want to see what I'm currently doing you can look at tex.stackexchange.com/questions/494687/… – tecosaur Jun 9 at 5:54
• So is this a duplicate of your previous question? – CarLaTeX Jun 9 at 5:59
• Not quite. I was a bit unsure as to whether this warranted a new question, but the way I see it is in that question, I'm asking 'why isn't this approach working?', here I'm trying to ask 'what would a good approach to this be?'. – tecosaur Jun 9 at 6:03

Here is a solution using cals. Since cals cell commands are in effect until they are turned off, it is easy to colourise rows. Just define commands for each colour you need using the definition of \gray I have used. Turn on \gray (or any other colours you have defined) before the first cell you want to be gray and turn it off (same command) when you want to the normal background.

Using background colour to emphasise rows, the toprule and midrule are unnecessary (I have not turned them off here). My tabular looks different since I do not have Plex Serif and Plex Sans in all the different weights.

To avoid that the second header line do not line up, I have explisitly line it up by a \vfil in the cells. In addition, I have loaded the package verbatimbox to allow the use of verbatimematerial in the cells.

\documentclass[a4paper]{article}
\usepackage{cals, xcolor}
\usepackage{verbatimbox}
\usepackage[margin=2.5cm]{geometry}

\newsavebox\myVerb
\newcommand*{\verbBox}{\usebox\myVerb}

\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{calstable}

\begin{verbbox}[\footnotesize]
\selectfont
\end{verbbox}

% Defining 3 column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(\columnwidth)/90*12\relax}
{\dimexpr(\columnwidth)/90*9\relax}
{\dimexpr(\columnwidth)/90*11\relax}
{\dimexpr(\columnwidth)/90*10\relax}
{\dimexpr(\columnwidth)/90*10\relax}
{\dimexpr(\columnwidth)/90*10\relax}
{\dimexpr(\columnwidth)/90*10\relax}
{\dimexpr(\columnwidth)/90*10\relax}
{\dimexpr(\columnwidth)/90*8\relax}
}
% The tabular fills the text area

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0pt}
\def\cals@bodyrs@width{0.4pt}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

\def\gray{\ifx\cals@bgcolor\empty
\def\cals@bgcolor{gray!30}
\else \def\cals@bgcolor{} \fi}

\def\tb{\ifx\cals@borderT\relax     % Top border switch (off-on)
\def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Botton border switch (off-on)
\def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\rb{\ifx\cals@borderR\relax     % Right border switch (off-on)
\def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax     % Left border switch (off-on)
\def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}

% R1
\brow
\gray\bb\lb\rb\cell{Typeface}
\cell{Bold}
\cell{Semibold}
\cell{Medium}
\cell{Text}
\cell{Regular}
\cell{Light}
\cell{Extra L}
\cell{Thin}\bb
\erow
% R2 Body
\brow
\ttfamily\footnotesize\cell{\vfil\theverbbox}
\cell{\vfil b}
\cell{\vfil sb}
\cell{\vfil mb}
\cell{\vfil tx}
\cell{\vfil m}
\cell{\vfil l}
\cell{\vfil el}
\cell{\vfil t}\rmfamily\small\gray
\erow
}
% R3 Body
\brow
\bb\cell{Plex Serif}
\cell{\bfseries Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\erow
% R4 Body
\brow
\itshape\cell{Plex Serif}
\cell{\bfseries Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\normalfont
\erow
% R5 Body
\brow
\cell{}
\cell{}
\cell{}
\cell{}
\cell{}
\cell{}
\cell{}
\cell{}
\cell{}
\erow
% R6 Body
\brow
\sffamily\cell{Plex Sans}
\cell{\bfseries Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\erow
% R7 Body
\brow
\itshape\cell{ Plex Sans}
\cell{\bfseries Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}
\cell{Word}\normalfont
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular

\end{document}

• Hmm. That's an interesting approach. It's a rather different way of doing tables but it looks like it has some merits. – tecosaur Jun 9 at 13:30
• @tecosaur It also has hooks just before and just after the cell, so you can program your own test. It has two limitations: No coloured rules and you have to specify the width of the columns. It is easier to do stuff on a per row than per column. – Sveinung Jun 9 at 15:26

Both \toprule and \midrule add some vertical space, so \rowcolor cannot fill completely the apparent cell spaces, but you can use a \specialrule to make ad hoc rules:

\documentclass{article}
\usepackage{geometry}
\usepackage[table]{xcolor}
\usepackage{booktabs,tabularx}
\renewcommand\toprule{\specialrule{1pt}{1pt}{0pt}\rowcolor{gray!10}}
\renewcommand\midrule{\specialrule{0.4pt}{0pt}{0pt}}
\begin{document}
\renewcommand\arraystretch{1.4}\tabcolsep3pt
\begin{tabularx}{\linewidth}{p{6em}XXXXXXXXXXX}
\toprule
\rowcolor{gray!10}
Typeface\par \verb|\selectfont| &
Bold\par  \verb|b| &
Semibold\par  \verb|sb|&
Medium\par \verb|mb|&
Text\par \verb|tx|&
Regular\par \verb|m|&
Light\par \verb|l|&
Extra L\par \verb|el|&
Thin\par \verb|t|\\
\midrule
Plex Serif & Words & Words & Words & Words
& Words & Words & Words & Words \\
Plex Sans & \sffamily Words & Words & Words
& Words & Words & Words & Words & Words\\
\bottomrule
\end{tabularx}
\end{document}

• Thanks for that Fran. I don't think I made it clear enough in the description that I'm looking to automaticly have the rows between a \toprule and a \midrule in all my tables are filled with a certain colour. However, I didn't know you could use \par that way so I absolutely learned something useful! – tecosaur Jun 10 at 2:08
• @tecosaur oh, that ...automatism ...of course, LaTeX is a macro language. See the updated answer. – Fran Jun 10 at 3:13