[Tex/LaTex] Table with partially colored cells

backgroundscolortables

I convinced my girlfriend to write her thesis with Latex, so now whenever she gets stuck I'm the go to guy. Sadly she has requested something of me which I don't know either. She needs a table in which some of the cells are colored as it can be seen in the following image.enter image description here

I created this using paint, but obviously I would rather use a correct way of doing it. I found both examples of how to get the diagonal lines into a cell and how to get colored cells, but I haven't been able to combine the two.

Does anyone know a solution to this? The other alternative I thought of would be to create the table using latex, convert to pdf, load it into inkscape, add the lines and the colors and include it as a figure again. This would mean that I need to redo it every time the table changes though. I'm hoping this can be done in a more elegant way!

Best Answer

Here's a solution using matrix of nodes from TikZ:

Code

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzset{limon/.style={fill=lime}}

\begin{tikzpicture}
    \matrix (magic) [matrix of nodes,nodes={minimum width=3cm,minimum height=1cm,draw,very thin},draw,inner sep=0]
    {   8 & 1 & 6 \\
        3 & |[limon]| 5 & |[limon]| 7 \\
        4 & 9 & 2 \\
    };
    \begin{pgfonlayer}{background}
        \fill[lime,draw=black] (magic-2-1.north east) -- (magic-2-1.west) -- (magic-2-1.south east) -- cycle;
    \end{pgfonlayer}

\end{tikzpicture}

\end{document}

Output

enter image description here

Related Question