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.

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!

share|improve this question
    
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. –  Peter Grill Oct 9 '12 at 21:14
    
Well there is always the tikz overkill solution... but perhaps others will come up with a simpler solution. –  Peter Grill Oct 9 '12 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 '12 at 21:44

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer
    
Thank you a lot! That looks great :D –  Pandoro Oct 9 '12 at 21:45
    
You're welcome. I don't know how familiar you are with TikZ, so don't hesitate to ask if anything is unclear. –  Tom Bombadil Oct 9 '12 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.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tikz}

\usetikzlibrary{calc,shadings}

% Andrew Stacey's code from
% http://tex.stackexchange.com/a/50054/3954
\makeatletter
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  }
}

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

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}

\colorlet{mycolor}{green!60!orange}

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

\newcommand\TriCell[2]{%
  \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 ;
  \end{tikzpicture}%
}%

\begin{document}

\TriCell{start1}{end1}
\begin{tabular}{|c|c|c|c|}
\hline
some text & some text & some text & some text \\
\hline
\multicolumn{1}{!{\vrule\tikzmark{start1}} c !{\vrule\tikzmark{end1}}}{some text} & \cellcolor{mycolor}some text & \cellcolor{mycolor}some text & \cellcolor{mycolor}some text \\
\hline
some text & some text & some text & some text \\
\hline
\end{tabular}

\end{document}

enter image description here

share|improve this answer
    
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 '12 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. –  Gonzalo Medina Oct 9 '12 at 21:50

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.