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!

  • Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems is fun, setting them up is not. Then those trying to help can simply cut/paste your MWE and get started on solving problem. In this case it is fairly simple, but if you can save those trying to help sometime that would be nice. Oct 9, 2012 at 21:14
  • Well there is always the tikz overkill solution... but perhaps others will come up with a simpler solution. Oct 9, 2012 at 21:17
  • Aaah, sorry for that. I had just finished my MWE when I saw the two answers. I will know this for the next time. Thank you for the valuable hint!
    – Pandoro
    Oct 9, 2012 at 21:44

3 Answers 3


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






    \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 \\
        \fill[lime,draw=black] (magic-2-1.north east) -- (magic-2-1.west) -- (magic-2-1.south east) -- cycle;




enter image description here

  • You're welcome. I don't know how familiar you are with TikZ, so don't hesitate to ask if anything is unclear. Oct 9, 2012 at 21:49

Here's one of the the overkill TikZ solutions mentioned by Peter Grill. I used a modification of this solution to Gradient color in one cell of a table.



% Andrew Stacey's code from
% https://tex.stackexchange.com/a/50054/3954
  remember picture with id/.style={%
    remember picture,
    save picture id=#1,
  save picture id/.code={%
  if picture id/.code args={#1#2#3}{%

  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%


  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya


\tikz[remember picture with id=#2] {#1;}}

  \begin{tikzpicture}[overlay,remember picture]%
    \fill[mycolor] ( $ (pic cs:#1) + (0pt,0.5ex) $ ) --  ( $ (pic cs:#2) + (0pt,1.9ex) $ ) -- ( $ (pic cs:#2) + (0pt,-0.8ex) $ ) --cycle ;


some text & some text & some text & some text \\
\multicolumn{1}{!{\vrule\tikzmark{start1}} c !{\vrule\tikzmark{end1}}}{some text} & \cellcolor{mycolor}some text & \cellcolor{mycolor}some text & \cellcolor{mycolor}some text \\
some text & some text & some text & some text \\


enter image description here

  • Thank you :) I would accept both answers if I could, but I like the other one just a bit more since it seems simpler to me. Thank you for the effort though :)
    – Pandoro
    Oct 9, 2012 at 21:46
  • 1
    @Pandoro You're welcome! No problem about accepting the other answer; I agree that it might be simpler if you're not interested in preserving the settings from a standard tabular. Oct 9, 2012 at 21:50

Here is a solution with {NiceTabular} of nicematrix.

That environment is similar to {tabular} (of array) but creates PGF/Tikz nodes under the cells, rows and columns of the array. It's possible to use these nodes in the \CodeBefore to draw whatever you want with Tikz before the construction of the array.



  \fill [lime] (2.5-|1) -- (3-|2) -- (3-|last) -- (2-|last) -- (2-|2) -- cycle ;
  \draw [line join = bevel] (2-|2) -- (2.5-|1) -- (3-|2) ;
  8 & 1 & 6 \\
  3 & 5 & 7 \\
  4 & 9 & 2 


You need several compilations (because nicematrix uses 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 .