2

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:

enter image description here

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?

enter image description here

2
  • 1
    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, 2017 at 21:51
  • 2
    for \cline see the colortbl documentation, it is explicitly not supported. (You could use \hhline from the hhline package instead) Jul 12, 2017 at 22:02

3 Answers 3

3

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} 

enter image description here

1
  • great. Pretty close on my code. I Like this. (Please not that the code is not completely marked/ indended as code)
    – flor1an
    Jul 13, 2017 at 0:13
6

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}

enter image description here

3
  • 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
    – flor1an
    Jul 13, 2017 at 0:07
  • 1
    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. Jul 13, 2017 at 6:19
  • What a clever solution! Thanks @UlrikeFischer for the idea.
    – topskip
    Oct 6, 2021 at 12:13
0

By using the environment {NiceTabular} instead of the environment {tabular} and the key colortbl-like (the syntax of colortbl will be using even though colortbl itself won't be used), we have directly the expected output.

\documentclass{report}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{cccc}[colortbl-like]
     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{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Ouput of the above code

You must log in to answer this question.

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