## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ How can I put different background colors in cells of a table?

Information and discussion about graphics, figures & tables in LaTeX documents.
jukaiser
Posts: 1
Joined: Wed Mar 27, 2019 3:46 pm

### How can I put different background colors in cells of a table?

I'm trying to change the background color of some cells that have values above some limit. For example, if the cell value is higher than 2, then the cell background color should be red. I think I'm very close because I can do it when the table only has numbers. The problem is I don't know how to do it when my table has a row with cells that contain text and the others with numbers. I always got the error "Missing number, treated as zero". When I remove the entire roll with text, I can do it perfectly. But I need this rows with text and I only want to change the background color of cells with numbers.

Anyone can help me?

This is my code:


Ijon Tichy
Posts: 8
Joined: Mon Dec 24, 2018 10:12 am
You could try, e.g.,
\multicolumn{1}{c}{Text}
for the text cell.

rais
Posts: 197
Joined: Sun Nov 16, 2014 8:51 pm
well, my first reaction was why not let LaTeX do it automagically?'
The first thing this \ApplyGradient of yours should do: check, if parameter given is a decimal (or real) number. xstring's \IfDecimal seemed perfect for the job.\textbf within it's argument threw me off course, though.
Ok, never mind, I just let \ApplyGradient decide, if the cell contents should be bold (string) or not (number).
Easy.
Come to think of it, all the other \textbf in the first column could be replaced by a single directive in the column specification (prepend >{\bfseries} to the first column specifier, c).
And what's with these \colorbox commands for colring a cell? I'd use \cellcolor, then I don't need to fiddle around with \tabcolsep and the like.
And twice the same color around \PercentColor? That looked a bit fishy to me...
Anyway, after putting some code in working order

I seriously doubt you'll be happy with it...let me put it this way: do you really want to apply the same colors with the same limits to Significant Wave Height' and, say, Wind Speed? I mean, 3 m may be a high wave, but 3 m/s is barely more than a light breeze...
Perhaps you should consider swapping columns with rows of this table (for using collcell) or forget collcell for this table (your columns don't contain stuff you could collect' this way) and use your \ApplyGradient` directly in the (few) cells where you actually need it. That seems to me less work than applying a \multicolumn to every cell you don't want colored this way.
Then again, I may be mistaken...

KR
Rainer