1

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

  • 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 '17 at 21:51
  • 2
    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
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

  • 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
4

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

  • 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
  • 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. – Ulrike Fischer Jul 13 '17 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.