1

I am trying to color-code cells of a large matrix that may either contain a number or a symbol. (MWE at end.) The numbers are supposed to be flush right within their cell, but the symbols are supposed to be centered. The normal way to do this is with \multicolumn. I find that this causes the colored box drawn by \cellcolor to extend not quite as far left as it should — sometimes. The "Wrong on the left" section of the screenshot below shows this problem; look closely where the red arrow points.

I tried to fix this by using some \hfill hackery instead of \multicolumn, but that had even worse effects; now the colored boxes consistently don't extend all the way to the right. The "Wrong on the right" section of the screenshot below shows this problem; there are no red arrows because it's not at all subtle.

How do I get the cell background boxes to be consistently all the same size?

Note 1: What I really want is for all of the data cells to be exactly the same width regardless of their contents.

Note 2: The real table (that the MWE is cut down from) is machine-generated, so I don't care how ugly the markup gets.

Note 3: The "symbols" in this MWE are < and 1.

screenshot of MWE, showing two different ways the background boxes can be the wrong size

\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\tabcolsep}{1pt}
\begin{document}

\section*{Wrong on the left}

\begin{tabular}{l@{\hspace{6pt}}r@{\hspace{6pt}}rrr}
& (size) & aaa & bbb & ccc \\
Aaaa aaaa & $5\,130$ &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} &\multicolumn{1}{c}{\cellcolor{blue!10}\color{black!50}$<$} &\cellcolor{blue!26}$.01$ \\
Bbbb bbbb & $7\,402$ &\multicolumn{1}{c}{\cellcolor{blue!10}\color{black!50}$<$} &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} &\cellcolor{blue!33}$.08$ \\
Cccc cccc & $1\,336$ &\cellcolor{blue!26}$.01$ &\cellcolor{blue!33}$.08$ &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} \\
\end{tabular}

\section*{Wrong on the right}

\begin{tabular}{l@{\hspace{6pt}}r@{\hspace{6pt}}rrr}
& (size) & aaa & bbb & ccc \\
Aaaa aaaa & $5\,130$ &\cellcolor{blue!75}$1$\hfill\hbox{} &\cellcolor{blue!10}\color{black!50}$<$\hfill\hbox{} &\cellcolor{blue!26}$.01$ \\
Bbbb bbbb & $7\,402$ &\cellcolor{blue!10}\color{black!50}$<$\hfill\hbox{} &\cellcolor{blue!75}$1$\hfill\hbox{} &\cellcolor{blue!33}$.08$ \\
Cccc cccc & $1\,336$ &\cellcolor{blue!26}$.01$ &\cellcolor{blue!33}$.08$ &\cellcolor{blue!75}$1$\hfill\hbox{} \\
\end{tabular}

\end{document}
zwol
  • 2,919

2 Answers2

2

Getting the bounds of \cellcolor right is a common problem. It doesn't adapt to differing column separators, so the @{\hspace{6pt}} throws it off. A workaround is to append the horizontal space to the preceding column, rather than modifying the column separation (use < instead of @ in your tabular specification).

As for making all the data cells the same size, I'd recommend defining a new column type, R, to set the width for all data columns (see this answer).

\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\tabcolsep}{1pt}
\newcolumntype{R}{>{\raggedleft\arraybackslash}p{0.25in}}
\begin{document}

\section*{Correct on both}

\begin{tabular}{l@{\hspace{6pt}}r<{\hspace{6pt}}RRR}
& (size) & aaa & bbb & ccc \\
Aaaa aaaa & $5\,130$ &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} &\multicolumn{1}{c}{\cellcolor{blue!10}\color{black!50}$<$} &\cellcolor{blue!26}$.01$ \\
Bbbb bbbb & $7\,402$ &\multicolumn{1}{c}{\cellcolor{blue!10}\color{black!50}$<$} &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} &\cellcolor{blue!33}$.08$ \\
Cccc cccc & $1\,336$ &\cellcolor{blue!26}$.01$ &\cellcolor{blue!33}$.08$ &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} \\
\end{tabular}

\end{document}

correct on both

Guho
  • 6,115
2

The package nicematrix has tools designed to address that kind of problem.

In the code of your first MWE, I have loaded nicematrix and replaced {tabular} by {NiceTabular} with the key color-inside (alias: colortbl-like).

The output is directly as expected (because, with nicematrix, the colored panels are drawn with PGF/Tikz and not with the mechanism of colortbl).

\documentclass{article}
\usepackage{xcolor}
\setlength{\tabcolsep}{1pt}

\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{l@{\hspace{6pt}}r@{\hspace{6pt}}rrr}[color-inside] & (size) & aaa & bbb & ccc \ Aaaa aaaa & $5,130$ &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} &\multicolumn{1}{c}{\cellcolor{blue!10}\color{black!50}$<$} &\cellcolor{blue!26}$.01$ \ Bbbb bbbb & $7,402$ &\multicolumn{1}{c}{\cellcolor{blue!10}\color{black!50}$<$} &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} &\cellcolor{blue!33}$.08$ \ Cccc cccc & $1,336$ &\cellcolor{blue!26}$.01$ &\cellcolor{blue!33}$.08$ &\multicolumn{1}{c}{\cellcolor{blue!75}$1$} \ \end{NiceTabular}

\end{document}

Output of the above code

Moreover, if there are several adjacent cells with exactly the same color, you won't see thin white lines between the cells, whatever PDF viewer you use.

F. Pantigny
  • 40,250