18

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

6
+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}
| improve this answer | |
15

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

| 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 '14 at 0:38
15

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

| improve this answer | |
3

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

| 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 '14 at 15:36
  • @MYaseen208 Couldn't you do it with the search & replace feature of your editor? – Old Nick Apr 29 '14 at 15:41
  • I'm using LyX. – MYaseen208 Apr 29 '14 at 16:10
  • @MYaseen208 LyX does have a search & replace feature. In the menubar go to Edit -> find & replace (Quick) or find & replace (Advanced) – Old Nick Apr 29 '14 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 '14 at 0:39
1

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}
| 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 '14 at 15:36
  • 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 '14 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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