LaTeX forum ⇒ Graphics, Figures & TablesHow 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?

Postby jukaiser » Wed Mar 27, 2019 3:56 pm

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:

  1. \documentclass{article}
  2. \usepackage[utf8]{inputenc}
  3. \usepackage{tikz}
  4. \usepackage{collcell}
  5.  
  6. \newcommand*{\MinNumber}{1.0}%
  7. \newcommand*{\MidNumber}{2.0} %
  8. \newcommand*{\MaxNumber}{3.0}%
  9.  
  10. %Apply the gradient macro
  11. \newcommand{\ApplyGradient}[1]{%
  12. \ifdim #1 pt > \MidNumber pt
  13. \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
  14. \hspace{-0.33em}\colorbox{red!\PercentColor!red}{#1}
  15. \else
  16. \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
  17. \hspace{-0.33em}\colorbox{green!\PercentColor!green}{#1}
  18. \fi
  19. }
  20.  
  21. \newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
  22. \renewcommand{\arraystretch}{0}
  23. \setlength{\fboxsep}{3mm} % box size
  24. \setlength{\tabcolsep}{0pt}
  25. \begin{document}
  26.  
  27. \vspace{1cm}
  28. \begin{figure}
  29. \centering
  30. %\includegraphics[scale=0.4]{fig/PNBOIA_vitoria.png} % Include the image placeholder.png
  31. \caption{Metocean forecast results.}
  32. %\caption{Espectro direcional}
  33. %\end{center}
  34. \end{figure}
  35.  
  36. \begin{table}
  37. \renewcommand{\baselinestretch}{2}
  38. \caption{Metocean Forecast Table.}
  39. \center
  40. \label{tabela_periodo}
  41. \begin{tabular}{| c | *{8}{R}}
  42. \hline
  43. \textbf{20180807} & \textbf{00h} & \textbf{03h} & \textbf{06h} & \textbf{09h} & %\textbf{12h} & \textbf{15h} & \textbf{18h} & \textbf{21h} \\ \hline
  44. \textbf{Significant Wave Height (m)} & 1.31 & 1.26 & 1.25 & 1.27 & 1.57 & 1.90 & 2.09 & 1.93 \\ \hline
  45. \textbf{Peak Period (s)} & 8.8 & 8.7 & 8.8 & 9.5 & 9.9 & 5.7 & 6.2 & 6.5 \\ \hline
  46. \textbf{Peak Direction (degree)} & 130 & 130 & 130 & 135 & 136 & 191 & 183 & 172 \\ \hline
  47. \textbf{Wind Speed (m/s)} & 1.0 & 1.1 & 3.0 & 7.4 & 8.9 & 10.4 & 10.9 & 8.2 \\ \hline
  48. \textbf{Wind Direction (degree)} & 36 & 359 & 261 & 224 & 199 & 194 & 188 & 170 \\ \hline
  49. \textbf{Tide} & 1.09 & 0.65 & 0.33 & 0.82 & 1.26 & 0.91 & 0.43 & 0.73\\ \hline
  50. \end{tabular}
  51. \end{table}
  52.  
  53.  
  54.  
  55. \end{document}

User avatar
Ijon Tichy
Posts: 8
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Sat May 11, 2019 3:45 pm

You could try, e.g.,
  1. \multicolumn{1}{c}{Text}
for the text cell.

rais
Posts: 197
Joined: Sun Nov 16, 2014 8:51 pm

Postby rais » Sun May 12, 2019 2:53 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
  1. \documentclass{article}
  2. \usepackage[utf8]{inputenc}
  3. \usepackage[table]{xcolor}
  4. \usepackage{tikz}
  5. \usepackage{collcell}
  6. \usepackage{xstring}
  7.  
  8. \newcommand*{\MinNumber}{1.0}%
  9. \newcommand*{\MidNumber}{2.0} %
  10. \newcommand*{\MaxNumber}{3.0}%
  11. \newcommand*\mynum{}% used inside \ApplyGradient; just checking if it exists already
  12.  
  13. %Apply the gradient macro
  14. \newcommand{\ApplyGradient}[1]{%
  15. \IfDecimal{#1}{%
  16. \edef\mynum{#1}%
  17. \ifdim #1 pt > \MaxNumber pt\relax
  18. \edef\mynum{\MaxNumber}%
  19. \else
  20. \ifdim #1 pt < \MinNumber pt\relax
  21. \edef\mynum{\MinNumber}%
  22. \fi
  23. \fi
  24. \ifdim \mynum pt > \MidNumber pt
  25. \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\mynum - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)}%
  26. \xdef\PercentColorr{\PercentColor}% must be global here if used in \cellcolor
  27. \cellcolor{red!\PercentColorr!yellow}#1%<- to have sth. of a `gradient'
  28. \else
  29. \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - \mynum)/(\MidNumber-\MinNumber),100.0),0.00)}%
  30. \xdef\PercentColorr{\PercentColor}% must be global here if used in \cellcolor
  31. \cellcolor{green!\PercentColorr!yellow}#1%
  32. \fi % \MinNumber < #1 <= \MaxNumber
  33. }{\textbf{#1}}% else it's not a decimal
  34. }
  35.  
  36. \newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
  37. \begin{document}
  38.  
  39. \begin{table}
  40. \renewcommand{\arraystretch}{2}
  41. \caption{Metocean Forecast Table.}
  42. \centering % `center' is defined as an environment
  43. \label{tabela_periodo}
  44. \begin{tabular}{|>{\bfseries} c | *{8}{R}}
  45. \hline
  46. 20180807 & 00h & 03h & 06h & 09h & 12h & 15h & 18h & 21h \\ \hline
  47. Significant Wave Height (m) & 1.31 & 1.26 & 1.25 & 1.27 & 1.57 & 1.90 & 2.09 & 1.93 \\ \hline
  48. Peak Period (s) & 8.8 & 8.7 & 8.8 & 9.5 & 9.9 & 5.7 & 6.2 & 6.5 \\ \hline
  49. Peak Direction (degree) & 130 & 130 & 130 & 135 & 136 & 191 & 183 & 172 \\ \hline
  50. Wind Speed (m/s) & 1.0 & 1.1 & 3.0 & 7.4 & 8.9 & 10.4 & 10.9 & 8.2 \\ \hline
  51. Wind Direction (degree) & 36 & 359 & 261 & 224 & 199 & 194 & 188 & 170 \\ \hline
  52. Tide & 1.09 & 0.65 & 0.33 & 0.82 & 1.26 & 0.91 & 0.43 & 0.73\\ \hline
  53. \end{tabular}
  54. \end{table}
  55.  
  56. \end{document}

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 1 guest