1

I'm trying to create a colored table with something like this code:

\documentclass[a4paper,twoside]{book} 
\usepackage{amsmath}
\usepackage{color}
\usepackage{tabularx}
\usepackage{colortbl}

\pagestyle{empty}
\definecolor{lightblue}{cmyk}{0.5,0,0,0}
\definecolor{lightgrey}{cmyk}{0.25,0,0.25,0}

\begin{document}

\begin{tabularx}{\textwidth}{|X|X|}
  \hline
  \rowcolor{lightblue}
  Item 1 & Item 2 \\ 
  \rowcolor{lightgrey}
  $$A=\left(\begin{smallmatrix}0&0&0\\0&0&0\end{smallmatrix}\right)$$ & 
  $$B=\left(\begin{smallmatrix}1&1&1\\1&1&1\end{smallmatrix}\right)$$ \\
  \hline
\end{tabularx}

\end{document}

But in the second row the first cell is completely black! Why?. I tried to change smallmatrix to matrix but then the cell shows a little black box. I don't understand nothing!

4
  • 1
    Don't use $$ but single $ for cells. $$ is for display math and greats a paragraph. It should be anyway not be used in modern LaTeX documents, but \[ .. \] instead. Commented Jul 12, 2011 at 11:46
  • I tried to do it, ...with the same result.
    – Phraellyn
    Commented Jul 12, 2011 at 12:00
  • I think the problem comes with the arraylike environments
    – Phraellyn
    Commented Jul 12, 2011 at 12:03
  • It wasn't meant as solution for this problem, but a general advice. Commented Jul 12, 2011 at 12:08

1 Answer 1

1

It looks like that the row color is reset incorrectly by the line end (\\) in the smallmatrix (\everycr resets the row color globally). You could use code like the following to save and restore the row color around the smallmatrix. This would be worth reporting to the authors of colortbl and amsmath.

\documentclass[a4paper,twoside]{book} 
\usepackage{amsmath}
\usepackage{color}
\usepackage{tabularx}
\usepackage{colortbl}

\pagestyle{empty}
\definecolor{lightblue}{cmyk}{0.5,0,0,0}
\definecolor{lightgrey}{cmyk}{0.25,0,0.25,0}


\makeatletter
\newenvironment{mysmallmatrix}{%
    \let\saved@CT@row@color\CT@row@color
    \begin{smallmatrix}%
}{%
    \end{smallmatrix}%
    \global\let\CT@row@color\saved@CT@row@color
}
\makeatother

\begin{document}

\begin{tabularx}{\textwidth}{|X|X|}
  \hline
  \rowcolor{lightblue}
  Item 1 & Item 2 \\ 
  \rowcolor{lightgrey}
  $\displaystyle A=\left(\begin{mysmallmatrix}0&0&0\\0&0&0\end{mysmallmatrix}\right)$ & 
  $\displaystyle B=\left(\begin{mysmallmatrix}1&1&1\\1&1&1\end{mysmallmatrix}\right)$ \\
  \hline
\end{tabularx}

\end{document}
3
  • Thank you again Martin! now works fine. You save my live two times this week.
    – Phraellyn
    Commented Jul 12, 2011 at 12:11
  • I supose I can define mymatrix and myarray environment in this way. Can I?
    – Phraellyn
    Commented Jul 12, 2011 at 12:12
  • ...Oh, ...the same solution don't work with matrix environment.
    – Phraellyn
    Commented Jul 12, 2011 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .