# Cell background color longer than midrule (or hline) in table

I have a small problem in my table editing using booktabs and colors. The main problem is the background color of a cell is longer than the midrule, which is not a huge problem but it is quite annoying to look at.

I use midrules, toprules and bottomrules. A row of the table is given as:

XXXXXX & xx\% & xx\% & xx\% & xx\% & \cellcolor[HTML]{CAFFCA}xx\% & \cellcolor[HTML]{CAFFCA}xx\% & \cellcolor[HTML]{CAFFCA}xx\% \\


I'm not sure how to extend the rules or how to shorten the color. Any advice is welcome.

Minimum working code:

\documentclass{article}

\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{multirow}

\begin{document}
\begin{table}
\centering
\footnotesize
\begin{tabular}{@{}l|rrrr@{}}
\toprule
\multicolumn{1}{c|}{\textbf{xx}} & \multicolumn{1}{c|}{\textbf{xx}} & \multicolumn{1}{c|}{\textbf{xx}} & \multicolumn{1}{c}{\textbf{xx}} & \multicolumn{1}{c}{\textbf{xx}} \\ \midrule
xx & xx & xx & xx & \cellcolor[HTML]{FE0000}xx \\
xx & xx & xx & xx & \cellcolor[HTML]{34FF34}xx \\ \bottomrule
\end{tabular}
\end{table}
\end{document}

• Please make a compilable minimal working example (MWE) that reproduces the output you show. – leandriis Apr 18 at 13:40
• If you use @{} in your column specification in order to remove horizonal white space at the right of your last table column, you might be interested in Color overshoots when using \cellcolor without intercolumn space – leandriis Apr 18 at 13:43
• I have added the minimum working example @leandriis – MathT Apr 18 at 13:53
• And the solution you proposed removes every space all around the table, making it extremely compact – MathT Apr 18 at 13:53
• – def Apr 18 at 13:55

In your case maybe a TikZ matrix can help.

With <matrixname>-<rownumber>-<columnnumber> you can refer to a single cell.

\documentclass{article}

\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{multirow}

\definecolor{myred}{HTML}{FE0000}
\definecolor{mygreen}{HTML}{34FF34}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{table}
\centering
\begin{tikzpicture}
\matrix[
matrix of nodes,
row sep=0pt,
column 1/.style={nodes={text width=3em, align=left}},
row 1/.style={nodes={text width=3em, align=center}},
nodes={text width=3em, align=center, font=\footnotesize}
] (M) {
xx & xx & xx & \textbf{xx} & \textbf{xx} \\[4pt]
xx & xx & xx & xx & |[fill=myred]|xx \\
xx & xx & xx & xx & |[fill=mygreen]|xx \\
};
% horizontal lines
\draw[thick] (M.north west) -- (M.north east);
\draw[thick] (M.south west) -- (M.south east);
\draw (M.west |- M-1-1.south west) -- (M-1-1.south west) -- (M-1-5.south east) -- (M.east |- M-1-5.south east);
% vertical line
\draw (M.north -| M-1-1.north east) -- (M-1-1.north east) -- (M-3-1.south east) -- (M.south -| M-3-1.south east);
\end{tikzpicture}
\end{table}
\end{document}