Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
1  
Related Question: Automatic coloring of numbers according to size. –  Peter Grill Apr 30 at 0:36

5 Answers 5

up vote 2 down vote accepted
+50

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}
share|improve this answer

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

share|improve this answer
    
I think the numbers should be in math mode to get the correct unary operator, so an array might be preferable. –  Peter Grill Apr 30 at 0:38

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

share|improve this answer

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

share|improve this answer
    
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 Apr 29 at 15:36
    
@MYaseen208 Couldn't you do it with the search & replace feature of your editor? –  dcmst Apr 29 at 15:41
    
I'm using LyX. –  MYaseen208 Apr 29 at 16:10
    
@MYaseen208 LyX does have a search & replace feature. In the menubar go to Edit -> find & replace (Quick) or find & replace (Advanced) –  dcmst Apr 29 at 16:18
    
I think the numbers should be in math mode to get the correct unary operator, so an array might be preferable. –  Peter Grill Apr 30 at 0:39

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|improve this answer
    
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. –  MYaseen208 Apr 29 at 15:36
    
Thanks for the feedback. –  Jesse Apr 29 at 15:38
    
I think the numbers should be in math mode to get the correct unary operator, so an array might be preferable. –  Peter Grill Apr 30 at 0:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.