Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer
    
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 '13 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 '13 at 17:44
    
@user4811 See the edited answer. –  Harish Kumar Dec 7 '13 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 '13 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 '13 at 17:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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