# Why does \rowcolors cut matrix delimiter?

I want to typeset a tabular with alterning row colors, containing matrices. Somehow, when adding \rowcolors{1}{}{black!5} the left bracket of some matrices is just omited. What am I doing wrong and how can I avoid this?

\documentclass[ border=2pt]{standalone}

\usepackage{amsmath}
\usepackage[natural,table]{xcolor}

\begin{document}
\rowcolors{1}{}{black!5}
\begin{tabular}[t]{ll}
\rowcolor{gray!50}{\bfseries SKP} & \\ \hline
$A$ & $\begin{bmatrix}-0.25 & 1 \\ 0 & 0.5 \end{bmatrix}$\\
$b=g$ & $\begin{bmatrix}0 \\ 1\end{bmatrix}$
\end{tabular}
\end{document}

• A syntax-related point, not directly related to your query: Writing \rowcolor{gray!50}{\bfseries SKP} &  may suggest that \bfseries SKP is the second argument of \rowcolor. To avoid creating such an impression, I think it's better to write \rowcolor{gray!50} \bfseries SKP & . The scope of \bfseries ends at &; there's thus no need to encase the cell's contents in curly braces. – Mico Feb 18 '16 at 8:26
• The brace is there, but apparently it is in a lower layer, so it's covered by the colored background. The main problem is that colortbl does many global assignments, that affect nested arrays inside a table. – egreg Feb 18 '16 at 10:07
• @egreg that seems feasible, but still the question is how to avoid this? Are there any other possibilities? – Ktree Feb 18 '16 at 10:13
• Apparently the problem is in the backspacing done by bmatrix for avoiding the space between the delimiter and the body of the matrix. – egreg Feb 18 '16 at 10:24

Here's what happens: bmatrix typesets the left delimiter, then the body of the matrix, with a backspace in between; now, the body is an array, so it inherits the current background color which each cell is printed with, including the padding normally done by colortbl.

The backspacing is the cause of the disappearance of the left delimiter, because it is covered by the new layer. Ah, the joys of color in TeX! ;-) The issue becomes apparent if pmatrix is used instead: we can clearly see that only part of the parenthesis is overprinted.

How to remedy? Define a colorbmatrix environment that reprints the missing delimiter.

\documentclass[border=2pt]{standalone}

\usepackage{amsmath}
\usepackage[natural,table]{xcolor}

\newsavebox{\bmatrixbox}
\newenvironment{colorbmatrix}
{\begin{lrbox}{\bmatrixbox}
\mathsurround=0pt
$\displaystyle \begin{bmatrix}} {\end{bmatrix}$%
\end{lrbox}%
\usebox{\bmatrixbox}%
\kern-\wd\bmatrixbox
\makebox[0pt][l]{$\left[\vphantom{\usebox{\bmatrixbox}}\right.$}%
\kern\wd\bmatrixbox
}

\begin{document}
\rowcolors{1}{}{black!5}
\begin{tabular}[t]{ll}
\rowcolor{gray!50}{\bfseries SKP} & \\ \hline
$A$ & $\begin{colorbmatrix}-0.25 & 1 \\ 0 & 0.5 \end{colorbmatrix}$\\
$b=g$ & $\begin{colorbmatrix}0 \\ 1\end{colorbmatrix}$ \\ \hline
$A$ & $\begin{colorbmatrix}-0.25 & 1 \\ 0 & 0.5 \end{colorbmatrix}$\\
\end{tabular}
\end{document}


In the row with the white background the (visible) left delimiter will be also overprinted, but I'm confident in TeX's accuracy.

The best would be avoiding colored background in tables altogether. But it's just my personal opinion.

• Thanks for this great answer! Again and again it's surprising how difficult some easy things are in tex. Didn't expect it would be necessary to redefine the bmatrix environment. I'm starting to ask myselft whether it would be better to avoid the usage of the standard amsmath matrices and move to tikz-based matrices like in tex.stackexchange.com/a/28452/92996 which I am using for beamer presentations anyway and of course also works in colored table lines. I'm impressed that there are many people who think table lines shouldn't be colored but I think it greatly improves their readability. – Ktree Feb 18 '16 at 12:33
• @Ktree I'm confident that LuaTeX will be able to do a much better color management. Unfortunately, TeX was born at a time when coloring printouts was not as standardized as it is today. Many things happened in the last 30+ years in the computing world. ;-) – egreg Feb 18 '16 at 12:39