6

How can I fill a table cell with different colors? I have tried the diagbox package but it doesn't work as you can see. I am not so much interested in the diagonal line. I only want to separate the cell in two triangles, put text in both triangles and fill them with different colors.

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{diagbox}

\begin{document}
\begin{tabularx}{8cm}{|X|X|X|X|}
  \hline
  \diagbox{\cellcolor{red}Time}{\cellcolor{green}Day} & Mon & Tue & Wed\\
  \hline
  Morning & used & used &\\
  \hline
  Afternoon &  & used & used\\
  \hline
\end{tabularx}
\end{document}

2 Answers 2

8

With mighty tikz and tikzmark:

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{tikz}
\newcommand\tikzmark[2][]{
  \tikz[remember picture,inner sep=\tabcolsep,outer sep=0,baseline=(#1.base),align=left]{\node[minimum width=\hsize](#1){$#2$};}
}
\begin{document}
\begin{tabularx}{8cm}{|X|X|X|X|}
  \hline
  \multicolumn{1}{|@{}X@{}|}{\tikzmark[a]{\raisebox{-1ex}{Time}\raisebox{1ex}{\hspace{1ex}Day}}} & Mon & Tue & Wed\\
  \hline
  Morning & used & used &\\
  \hline
  Afternoon &  & used & used\\
  \hline
\end{tabularx}
\begin{tikzpicture}[remember picture,overlay]
\path[fill=red,opacity=0.2](a.north west)--(a.south west) -- (a.south east) -- cycle;
\path[fill=green,opacity=0.2](a.north east)--(a.south east) -- (a.north west) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

With overlay, it is difficult to set the background. You can draw the nodes once again instead (though it looks ugly)

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{tikz}
\newcommand\tikzmark[2][]{
  \tikz[remember picture,inner sep=\tabcolsep,outer sep=0pt,baseline=(#1.base),align=left]{\node[minimum width=\hsize](#1){$#2$};}
}
\usetikzlibrary{positioning}
\begin{document}
\begin{tabularx}{8cm}{|X|X|X|X|}
  \hline
  \multicolumn{1}{|@{}X@{}|}{\tikzmark[a]{\raisebox{-2.5ex}{Time}\raisebox{1ex}{\hspace{0.9ex}Day}}} & text text text text & Tue & Wed\\
  \hline
  Morning & used & used &\\
  \hline
  Afternoon &  & used & used\\
  \hline
\end{tabularx}
\begin{tikzpicture}[remember picture,overlay]
\path[fill=red,opacity=1](a.north west)--(a.south west) -- node [pos=.5, above left=0.5ex and -0.75ex] {Time}(a.south east) -- cycle ;
\path[fill=green,opacity=1](a.north west)-- node [pos=.5, below right=0.5ex and 0.1ex] {Day}(a.north east)-- (a.south east)  -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

Adjust the ex values suitably.

5
  • Thanks a lot. The problem is that this solution only works if the "Time/Day" cell is the "biggest" cell. In my "real-world" table this is sometimes not the case. You can see the problem by replacing "Mon" with "text text text text" in the table above. Is there a solution for this?
    – user4811
    Dec 7, 2013 at 12:41
  • Thanks, but there is still one little problem. The background color in the multi-color cell is transparent. Is it possible to set opacity=1 and say Tikz to fill only the background?
    – user4811
    Dec 7, 2013 at 17:44
  • @user4811 See the edited answer.
    – user11232
    Dec 7, 2013 at 22:56
  • It is also possible to do it without tikzmark tex.stackexchange.com/questions/149011/…, but your solution have brought me on the right track. Thx for your help!
    – user4811
    Dec 9, 2013 at 22:40
  • Nope, it also works with tabularx. The trick is to draw an empty Tikz node which just reserves the space. Then you can put whatever you want in this node.
    – user4811
    Dec 10, 2013 at 17:33
1

Here is a solution with {NiceTabularX} of nicematrix. That environment is similar to the classical environment {tabularx} (of the eponymous package) but creates PGF/Tikz nodes under the cells, rows and columns. It's possible to use these nodes with Tikz to draw or fill whatever you want in the tabular.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\renewcommand{\arraystretch}{1.4}

\begin{NiceTabularX}{8cm}{XXXX}[hvlines]
\CodeBefore
  \begin{tikzpicture}
     \fill [red!15] (1-|1) |- (2-|2) -- cycle ;
     \fill [green!15] (1-|1) -| (2-|2) -- cycle ;
  \end{tikzpicture}
\Body
  \diagbox{Time}{Day} & Mon  & Tue  & Wed  \\
  Morning             & used & used &      \\
  Afternoon           &      & used & used \\
\end{NiceTabularX}

\end{document}

You need several compilations (because of the PGF/Tikz nodes).

Output of the above code

You must log in to answer this question.

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