[Tex/LaTex] LaTeX Tables: Cell value color based on its sign / conditional cell color

colorformattingtables

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

Best Answer

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}