# Background colour for cells with padding

I have a pretty complex table where I had to add some borders and some padding. Now I want to colour these "special" cells with a background. But the colour is not expanding to the padding. Are there any way to extend it? this is my MWE code:

\documentclass{report}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{cccc}

a & b & & d \\
&   & &   \\ \cline{1-2}
\multicolumn{1}{|@{\hspace{3em}}c@{\hspace{1em}}}{\cellcolor{red!50} E }
&
\multicolumn{1}{@{}c@{\hspace{3em}}|}{\cellcolor{red!50}  F  }
&   &  h
\\[15pt] \cline{1-2}
\end{tabular}

\end{document}


and this his how it appears:

the yellow highlighted parts in the next image should get coloured too. Further I discovered, that the border on the top is missing. Why? How can I get her back?

• If I remember correctly, \cellcolor has overhang (padding) arguments, see colortbl documentation please (colortbl is loaded by xcolor when table option is used) – user31729 Jul 12 '17 at 21:51
• for \cline see the colortbl documentation, it is explicitly not supported. (You could use \hhline from the hhline package instead) – David Carlisle Jul 12 '17 at 22:02

Here are two ways to obtain it. Note hhline is required with coloured cells.

\documentclass{report}
\usepackage[table]{xcolor}
\usepackage{booktabs, hhline}

\begin{document}

\begin{tabular}{cccc}
a & b & & d \\
& & & \\ \hhline{--~~}
\multicolumn{1}{| @{\color{red!50}\vrule width 3em}c@{\color{red!50}\vrule width 1em}}{\cellcolor{red!50} E }
&
\multicolumn{1}{@{}c@{\color{red!50}\vrule width 3em}|}{\cellcolor{red!50} F }
& & h
\\[15pt] \cline{1-2}
\end{tabular}
\vspace{1cm}

\begin{tabular}{cccc}
a & b & & d \\
& & & \\ \hhline{--~~}%
\multicolumn{1}{|@{\hspace{3em}}>{\columncolor{red!50}[3em][1em]}c@{\hspace{1em}}}{ E }
&
\multicolumn{1}{@{}>{\columncolor{red!50}[0.4pt][3em]}c@{\hspace{3em}}|}{ F }
& & h
\\[15pt] \cline{1-2}
\end{tabular}

\end{document}


• great. Pretty close on my code. I Like this. (Please not that the code is not completely marked/ indended as code) – user1234 Jul 13 '17 at 0:13

In general I do such things with tikz and \tikzmark. The horizontal coordinates are normally quite easy to get. The vertical need sometimes some calculation or more \tikzmarks. The neat thing is that you can draw the background before the tabular and so it is really always in the background.

\documentclass{report}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\tikz[overlay,remember picture]
\fill[red!50] ([yshift=-\dimexpr\dp\strutbox+15pt]pic cs:A)
rectangle ([yshift=\ht\strutbox]pic cs:B);

\begin{tabular}{cccc}

a & b & & d \\
&   & &   \\ \cline{1-2}
\multicolumn{1}{|@{\tikzmark{A}\hspace{3em}}c@{\hspace{1em}}}{ E }
&
\multicolumn{1}{@{}c@{\hspace{3em}\tikzmark{B}}|}{  F  }
&   &  h
\\[15pt] \cline{1-2}
\end{tabular}

\end{document}


• it looks really nice, but for some reason it adds a blank page (not with the MWE, but within my doc) somehow caused by the two lines right after \begin{document}, but I will try to fix it – user1234 Jul 13 '17 at 0:07
• The tikz code is a small zero sized box but nevertheless a box. So put it in a place where such a box doesn't do harm eg after a word. – Ulrike Fischer Jul 13 '17 at 6:19