35

In a LaTeX table with merged cells, how can I color each cell with a different color?

alt text

%% LyX 1.6.7 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

\makeatother

\usepackage{babel}

\begin{document}
\begin{tabular}{|c|c|c|c|}
\cline{3-4} 
\multicolumn{1}{c}{} &   & \multicolumn{2}{c|}{Overexpression}\tabularnewline
\cline{3-4} 
\multicolumn{1}{c}{} &  & Cancer & Control \tabularnewline
\hline 
Marked & Cancer  & 45  & 61\tabularnewline
\cline{2-4} 
Cell Line & Control  & 42  & 52\tabularnewline
\hline
\end{tabular}
\end{document}

3 Answers 3

40
\usepackage[table]{xcolor}
...   
\begin{tabular}{...}
\cellcolor{blue} foo & \cellcolor{red} bar\\

...

There is also a command for coloring whole columns and rows.

10

Put \usepackage[table]{xcolor} in the preamble, use nested tables & these commands:

\rowcolor{}, \columncolor{}

3
  • Will this work well on merged cells?
    – denilw
    Commented Jan 13, 2011 at 18:59
  • @denilw: I don't use merged cells. I use nested tables instead. Commented Jan 14, 2011 at 0:43
  • 1
    Hi @xport,that is an interesting idea, could you provide an example of how to do my example with nested tables, and include the colouring?
    – denilw
    Commented Jan 15, 2011 at 20:18
1

With {NiceTabular} of nicematrix, you can do that table easily and color easily cells and also merged cells with a perfect result in the PDF viewers.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{cccc}[hvlines,corners=NW,color-inside]
          &         & \Block[fill=red!15]{1-2}{Overexpression} \\
          &         & Cancer & Control \\
\Block[fill=red!15]{2-1}{}%
Marked    & Cancer  & 45  & 61 \\
Cell Line & Control & 42  & \cellcolor{blue!10} 52 \\
\end{NiceTabular}
\end{document}

You need several compilations (because nicematrix uses PGF/Tiks nodes under the hood).

Output of the above code

You must log in to answer this question.

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