LaTeX Tables: Cell value color based on its sign / conditional cell color

5,755

Solution 1

This could work in your document. But it is a bit drastic and may well break some things.

colored cells

\documentclass{article}
\usepackage{color}
\usepackage{etoolbox}

\begingroup
\lccode`~`-
\lowercase{%
\endgroup\pretocmd{\tabular}{\catcode`~\active\def~{\color{red}-}}{}{}}

\begin{document}
Not colored here: -12, But colored within the tabular
\begin{tabular}{ l c r }
  1 & 2 & 3 \\
  -4 & 5 & 6 \\
  7 & 8 & -9 \\
\end{tabular}
and again not colored here: -12.

\end{document}

Solution 2

If your matrices only have numeric entries, you can do with collcell:

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

\newcommand{\checkvalue}[1]{\ifnum#1<0 \cellcolor{red}\fi#1}

\newcolumntype{L}{>{\collectcell\checkvalue}l<{\endcollectcell}}
\newcolumntype{C}{>{\collectcell\checkvalue}c<{\endcollectcell}}
\newcolumntype{R}{>{\collectcell\checkvalue}r<{\endcollectcell}}

\begin{document}
\[
\begin{array}{ L C R }
   1 &  2 &  3 \\
  -4 &  5 &  6 \\
   7 &  8 & -9 \\
   2 & -7 &  0
\end{array}
\]
\end{document}

enter image description here

Solution 3

This answer extends dcmst's answer. It defines new column types L, C, R that check, if the first token is - and sets \cellcolor{red} in this case. Therefore the table cells do not need to be changed, only the column specifications from lowercase to uppercase letters.

Example:

\documentclass{article}
\usepackage{array}
\usepackage{colortbl}

\makeatletter
\newcommand*{\minuscellcolor}{}
\def\minuscellcolor\ignorespaces{%
  % \ignorespaces not really needed, because \@ifnextchar gobbles spaces
  \@ifnextchar-{\cellcolor{red}}{}%
}
\newcolumntype{L}{>{\minuscellcolor}l}
\newcolumntype{C}{>{\minuscellcolor}c}
\newcolumntype{R}{>{\minuscellcolor}r}
\makeatother

\begin{document}
\begin{tabular}{ L C R }
  1       & 2 & 3       \\
  -4 & 5 & 6       \\
  7       & 8 & -9 \\
  2 & -7 & 0 \\
\end{tabular}

\end{document}

Result

Solution 4

You could define a \minus command that sets the cell color to red and also adds a "-" symbol, then use search & replace to replace all the istances of "-" with "\minus".

Like this:

\documentclass{article}
\usepackage{colortbl}
\newcommand{\minus}{\cellcolor{red}-}

\begin{document}    
\begin{tabular}{ l c r }
  1       & 2 & 3       \\
  \minus4 & 5 & 6       \\
  7       & 8 & \minus9 \\
\end{tabular}

\end{document}

enter image description here

Solution 5

An alternative: use of ifnum test. Note: since there are negative numbers, flush right is preferred. i.e. [rrr]

enter image description here

Code

\documentclass[border=10pt]{standalone}
\usepackage{xcolor,colortbl}
\begin{document}
\newcommand\mycolor[1]{
\ifnum #1<0 \cellcolor{red}#1%
\else
#1%
\fi
}
\begin{tabular}{ l c r }
 \mycolor{1} & 2 & 3 \\
 \mycolor{-4} & 5 & 6 \\
  7 & 8 &\mycolor{-9} \\
\end{tabular}

\end{document}
Share:
5,755

Related videos on Youtube

MYaseen208
Author by

MYaseen208

Updated on June 26, 2022

Comments

  • MYaseen208
    MYaseen208 less than a minute

    I've a long LaTeX document with many tables. Now I want to show negative values in cell in red color. I wonder how to do globally. My MWE is below:

    \documentclass{article}
    
    \begin{document}
    
    \begin{tabular}{ l c r }
      1 & 2 & 3 \\
      -4 & 5 & 6 \\
      7 & 8 & -9 \\
    \end{tabular}
    
    \end{document}
    

    Edited

    Thanks all who provided answers. From provided answers it seems that I have to make edits in my all tables which have negative values and there is no automated solution. But still I will keep this question for better solution. One more clarification, I need negative values in red not their background. Thanks

  • MYaseen208
    MYaseen208 about 8 years
    Thanks @dcmst for your nice answer but, for me, this answer has a limitation. Need to make changes in many many tables of long document.
  • MYaseen208
    MYaseen208 about 8 years
    Thanks @Jesse for your nice answer but, for me, this answer has a limitation. Need to make changes in many many tables of long document.
  • Old Nick
    Old Nick about 8 years
    @MYaseen208 Couldn't you do it with the search & replace feature of your editor?
  • MYaseen208
    MYaseen208 about 8 years
    I'm using LyX.
  • Old Nick
    Old Nick about 8 years
    @MYaseen208 LyX does have a search & replace feature. In the menubar go to Edit -> find & replace (Quick) or find & replace (Advanced)
  • Peter Grill
    Peter Grill about 8 years
    I think the numbers should be in math mode to get the correct unary operator, so an array might be preferable.
  • Peter Grill
    Peter Grill about 8 years
    I think the numbers should be in math mode to get the correct unary operator, so an array might be preferable.
  • Peter Grill
    Peter Grill about 8 years
    I think the numbers should be in math mode to get the correct unary operator, so an array might be preferable.