# Booktabs Cell Coloring Overflow and Position Bias

I want to fill booktabs table cells in a custom manner. I did two examples as below, one with kerning and the other is straight booktabs table. The first problem is in both tables, coloring that is not at the table border overflows up to the border of the next column (the red line in the image indicates this). The second problem is kerning macros (or whatever they are called), distracts the positioning of cell texts, as seen in the image. The texts are biased to bottom of the cell.

• How can I prevent overflowing in single cell coloring?
• How can I remove position bias? (Maybe this can be inherently fixed after finding a solution to the first question)

\begin{table}[b!]
\centering
\caption{Kern'ed Table.}
\label{tab:graphExplanation}
%\renewcommand{\arraystretch}{1.15}
\extrarowheight=\aboverulesep
\aboverulesep=0pt
\belowrulesep=0pt
\begin{tabular}{>{\kern-\tabcolsep}cccc<{\kern-\tabcolsep}}
\toprule

\multicolumn{2}{c}{MulC1}
& \multicolumn{2}{c}{MulC2}  \\
\cmidrule(r){1-2}
\cmidrule(r){3-4}

\textbf{Col1} & \textbf{Col2} & \textbf{Col1} & \textbf{Col2}  \\

Cell & \cellcolor[HTML]{C0C0C0} Cell & Cell & \cellcolor[HTML]{C0C0C0}Cell \\

Cell &  \cellcolor[HTML]{C0C0C0}Cell & Cell &  \cellcolor[HTML]{C0C0C0}Cell \\
\bottomrule
\end{tabular}
\end{table}
\begin{table}[b!]
\centering
\caption{UnKern'ed Table.}
\label{tab:graphExplanation}
%\renewcommand{\arraystretch}{1.15}

\begin{tabular}{@{}cccc@{}}
\toprule

\multicolumn{2}{c}{MulC1}
& \multicolumn{2}{c}{MulC2}  \\
\cmidrule(r){1-2}
\cmidrule(r){3-4}

\textbf{Col1} & \textbf{Col2} & \textbf{Col1} & \textbf{Col2}  \\

Cell & \cellcolor[HTML]{C0C0C0} Cell & Cell & \cellcolor[HTML]{C0C0C0}Cell \\

Cell &  \cellcolor[HTML]{C0C0C0}Cell & Cell &  \cellcolor[HTML]{C0C0C0}Cell \\
\bottomrule
\end{tabular}
\end{table}

New contributor
Erdem Tuna is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Welcome to TeX.SX! If you shorten the right side of the gray boxes in the second column, its contents will look off centered. – leandriis yesterday
• I can see that the contents in your first example are not centered vertically but off set more to the bottom of the cell. What I was referring to in my previour comment is the horizontal alignment. If you remove part of the gray background the contents of the colored cells appear aligned to the right instead of horizontally centered. – leandriis yesterday
• You can quite easily achieve an output such as the following: i.stack.imgur.com/pJaif.png In this example, the contents are indeed centered vertically, solving your second issue. However, I did not use booktabs, thus there is a solid instead of a split line below the headers. – leandriis yesterday
• The off centering to the bottom of the cell is due to \extrarowheight. If you really want to centre vertically, I sugest using the cellspace package instead: it ensures a minimal vertical spacing at the top and bottom of cells in a column. – Bernard yesterday
• @ErdemTuna have a look at the excellent nicematrix package below in the answer – js bibra yesterday

Here are two examples using an empty column between the first two and the last two columns:

Instead of lines from the booktabs package, I used regular \hline and \cline commands. In order to symmetrically increase the row heights of the table, I either used the cellspace package or the corresponsing options from the nicematrix package.

You can of course change the spacing above and below the text, as well as left and right of the text in a cell to your liking. Adding \setlength{\tabcolsep}{3pt} will for example result in the following output:

\documentclass{article}

%%%%%%%%%% used in the first example %%%%%%%%%%%%%%%%%
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}
\usepackage{colortbl}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%% used in the second example %%%%%%%%%%%%%%%%
\usepackage{nicematrix}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{tabular}{0c0c0l0c0c}
\hline
\multicolumn{2}{0c}{MulC1}
&& \multicolumn{02}{c}{MulC2}  \\
\cline{1-2} \cline{4-5}

\textbf{Col1} & \textbf{Col2} && \textbf{Col1} & \textbf{Col2}  \\

Cell & \cellcolor[HTML]{C0C0C0}Cell && Cell & \cellcolor[HTML]{C0C0C0}Cell \\

Cell &  \cellcolor[HTML]{C0C0C0}Cell && Cell &  \cellcolor[HTML]{C0C0C0}Cell \\
\hline
\end{tabular}

\bigskip

\NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt}
\begin{NiceTabular}{cclcc}[code-before =\cellcolor{lightgray}{3-2,3-5,4-2,4-5}]
\hline
\multicolumn{2}{c}{MulC1}
&& \multicolumn{2}{c}{MulC2}  \\
\cline{1-2} \cline{4-5}

\textbf{Col1} & \textbf{Col2} && \textbf{Col1} & \textbf{Col2}  \\

Cell & Cell && Cell & Cell \\

Cell &  Cell && Cell &  Cell \\
\hline
\end{NiceTabular}

\end{document}


With the excellent nicematrix package there is no problem

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

\begin{document}
\begin{NiceTabular}{lccc}[code-before=\cellcolor{blue!30}{3-2,4-2,3-4,4-4}]
\toprule[2pt]
\Block{1-2}   {MulC1}     && \Block{1-2}{MulC2} \\
\cmidrule(l){1-2}        \cmidrule(l){3-4}
\textbf{Col1} & \textbf{Col2} & \textbf{Col1} & \textbf{Col2}  \\
Cell &  Cell & Cell & Cell \\
Cell &  Cell & Cell &  Cell \\
\bottomrule[2pt]
\end{NiceTabular}
\end{document}


Just guessing (to long for comment). Do you like to have the following table:

For it is using part of each table. Also are changed \cmidrules:

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}
\begin{table}[b!]
\centering
\caption{Kern'ed Table.}
\label{tab:graphExplanation}
\begin{tabular}{>{\kern-\tabcolsep}cccc }
\toprule
\multicolumn{2}{c}{MulC1}                   & \multicolumn{2}{c}{MulC2}         \\
\cmidrule{1-2}
\cmidrule(l){3-4}

\textbf{Col1}   & \textbf{Col2}             & \textbf{Col1}
& \textbf{Col2}             \\
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
\specialrule{\heavyrulewidth}{0pt}{2pt}
\end{tabular}
\end{table}
\end{document}


Edit:

• content of last column is centered
• bottomrule is replaced by \specialrule

Addendum: IN the case, that you like to add \midrule between column headers and table body, you can do this with \specialrule{\lightrulewidth}{2pt}{0pt}:

\begin{tabular}{>{\kern-\tabcolsep}cccc }
\toprule
\multicolumn{2}{c}{MulC1}                   & \multicolumn{2}{c}{MulC2}         \\
\cmidrule{1-2}
\cmidrule(l){3-4}

\textbf{Col1}   & \textbf{Col2}             & \textbf{Col1}
& \textbf{Col2}             \\
\specialrule{\lightrulewidth}{2pt}{0pt}
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
Cell            & \cellcolor{gray!50}{Cell} & Cell  & \cellcolor{gray!50}{Cell} \\
\specialrule{\heavyrulewidth}{0pt}{2pt}
\end{tabular}


which gives:

• I actually wanted to completely fill a cell without any overflow and causing off centering – Erdem Tuna yesterday
• @ErdemTuna, do you like to remove gap at bottom line? It is easy to achieve by use special rule described in booktabs package, page 6. See edited answer! – Zarko yesterday
• When I add midrule between second and third, it doesn't fill whole cell. Is it possible to fix that? – Erdem Tuna yesterday
• @ErdemTuna, yes, with use of the special rule: \specialrule{\lightrulewidth}{2pt}{0pt}. I will add this as addendum to answer asap. – Zarko yesterday