Tx · LaTeX & TikZ

How to Color LaTeX Table Cells Based on a Number’s Sign

Published By the 9to5Science editorial team

At a glance

  1. 1. Load color support

    Load xcolor with its table option, plus PGF for numerical comparisons.

  2. 2. Test the value

    Compare each numeric cell with zero to classify it as negative, positive, or zero.

  3. 3. Apply the style

    Use the comparison result to choose the cell background and text color.

  4. 4. Check the output

    Compile and verify that each cell’s displayed value matches the color rule.

You can color table cells conditionally by testing each value before printing it. A practical approach is to load xcolor for cell and text colors, then use PGF’s math parser to decide whether a numeric value is negative, positive, or zero.

The example below defines a command named \signcell. Use it in place of a number in a table: negative values get a pale red background, positive values get pale green, and zero gets gray.

Complete example

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

\newcommand{\signcell}[1]{%
  \pgfmathtruncatemacro{\cellnegative}{(#1)<0}%
  \ifnum\cellnegative=1
    \cellcolor{red!12}\textcolor{red!70!black}{#1}%
  \else
    \pgfmathtruncatemacro{\cellpositive}{(#1)>0}%
    \ifnum\cellpositive=1
      \cellcolor{green!12}\textcolor{green!40!black}{#1}%
    \else
      \cellcolor{gray!12}\textcolor{black}{#1}%
    \fi
  \fi
}

\begin{document}

\begin{tabular}{lr}
  \hline
  Item & Change \\
  \hline
  A & \signcell{-2.4} \\
  B & \signcell{0} \\
  C & \signcell{3.1} \\
  \hline
\end{tabular}

\end{document}

The [table] option for xcolor enables table-cell coloring. PGF’s \pgfmathtruncatemacro evaluates a comparison and stores its result as an integer: 1 when the comparison is true and 0 when it is false. The two \ifnum tests select the style. The final \else branch handles zero, since zero is neither less than nor greater than zero.

The command both colors the cell background with \cellcolor and colors the printed number with \textcolor. If you want background shading only, remove the \textcolor{...}{...} wrapper in each branch and print #1 directly.

How to use the command

Put a plain numeric value inside the braces, such as \signcell{-0.25} or \signcell{18}. The command compares that value with zero and prints the same value you supplied. In the example, the numbers are placed in a right-aligned r column, which is suitable for a short list of values.

To change the palette, replace the color specifications. For example, blue!10 means a mixture containing 10 percent blue and 90 percent white. You can also define named colors with \definecolor if you want consistent colors throughout a document. Keep enough contrast between text and background for the numbers to remain readable, including in print or when viewed by readers with color-vision differences.

If you only have a few cells, you do not need a conditional command: add \cellcolor{red!12} or another color directly to each cell. A macro is useful when values change often or the same rule must be applied throughout a table.

Common mistakes

  • Leaving out the table option: Use \usepackage[table]{xcolor} so \cellcolor is available in table cells. If another package already loads xcolor, avoid loading it again with incompatible options; load the needed option before that package or use the package’s documented option-passing method.
  • Passing nonnumeric content: The argument must be a number PGF can parse. A header such as Change or formatted text such as $-2.4$ is not a suitable argument to \signcell. Keep headers outside the command and pass the bare value, as in \signcell{-2.4}.
  • Treating zero as positive: A test for less than zero alone does not distinguish positive values from zero. The example tests both comparisons and gives zero its own neutral style.
  • Expecting semantic color to be the only signal: Color may be lost in grayscale printing or inaccessible to some readers. Where the distinction matters, include a sign, label, or other non-color cue as well.
  • Using the macro in a parsed-number column without checking: The example prints color commands as part of each cell, so it is intended for ordinary l, c, or r columns. Numeric column packages may parse cell contents before typesetting; check that package’s documentation for its supported way to apply cell colors, or use a separate display column.

For a table with decimal values that also need to align on the decimal point, keep the comparison and visual formatting separate from the alignment mechanism. Test your exact table setup after changing column types, since numeric parsing and cell-color commands can interact.

References

  1. The PGF/TikZ Manual
  2. The xcolor Package

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.