1

I have a table (implemented through tabular) that uses the xcolor package to shade certain cells. The shading I'm interested is both entire cell shading (e.g. \cellcolor{red}) and element shading (e.g. \color{red}).

What I want to achieve is a function through xcolor (or similar package) that would allow me to implement a color of varying intensity. For example it would be nice if I could go \color{red}{0.01} in a cell, and the contents were only slightly red (but still almost black), or \color{red}{1.00} and the contents would be completely red.

How can I achieve this with both \cellcolor{red} and \color{red}?

Here's an example table that I want to achieve this in:

\begin{table}[!tbp]
\begin{center}
\begin{tabular}{rrrrl}
\hline\hline
\multicolumn{1}{c}{Sepal.Length}&\multicolumn{1}{c}{Sepal.Width}&\multicolumn{1}{c}{Petal.Length}&\multicolumn{1}{c}{Petal.Width}&\multicolumn{1}{c}{Species}\tabularnewline
\hline
   $5.1$&   $3.5$&   $1.4$&   $0.2$&   setosa\tabularnewline
   $4.9$&\cellcolor{red}   $3.0$&\color{red}   $1.4$&   $0.2$&   setosa\tabularnewline
   $4.7$&   $3.2$&   $1.3$&   $0.2$&   setosa\tabularnewline
   $4.6$&   $3.1$&   $1.5$&   $0.2$&   setosa\tabularnewline
  $5.0$&   $3.6$&   $1.4$&   $0.2$&   setosa\tabularnewline
   $5.4$&   $3.9$&   $1.7$&   $0.4$&   setosa\tabularnewline
\hline
\end{tabular}
\end{center}
\end{table}

I am mainly looking for a solution that keeps the "purity" of red (i.e. when I reduce the intensity it's still red it just has white mixed in).

user2763361
  • 435
  • 2
  • 11

1 Answers1

1

The specs in your question differ from those in your comment; I'm not sure what the syntax of your command should be. See if my code below answers your question. Otherwise, plese be more specific.

enter image description here

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

\newcommand\cellRedBG[1]
  {\cellcolor{red!#1!white}}

\newcommand\cellRedFG[1]
    {\color{red!#1!black}}

\begin{document}
\begin{center}
\begin{tabular}{rrrrl}
\hline\hline
\multicolumn{1}{c}{Sepal.Length}&
\multicolumn{1}{c}{Sepal.Width}&
\multicolumn{1}{c}{Petal.Length}&
\multicolumn{1}{c}{Petal.Width}&
\multicolumn{1}{c}{Species}
\tabularnewline
\hline

     $5.1$&   $3.5$&   $1.4$&   $0.2$&   setosa\tabularnewline
   $4.9$& \cellRedBG{20}   $3.0$&\cellRedFG{75}   $1.4$&   $0.2$&   setosa\tabularnewline
   $4.7$&   $3.2$&   $1.3$&   $0.2$&   setosa\tabularnewline
   $4.6$&   $3.1$&   $1.5$&   $0.2$&   setosa\tabularnewline
  $5.0$&   $3.6$&   $1.4$&   $0.2$&   setosa\tabularnewline
   $5.4$&   $3.9$&   $1.7$&   $0.4$&   setosa\tabularnewline
\hline
\end{tabular}
\end{center}
\end{document}
jub0bs
  • 58,916