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}

Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests