# Adding background color to table cells in LyX

I'm doing some tables using LyX and I tried to set a color background in the cells , but I got this error:

Undefined control sequence

\rowcolor
{cyan} &
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx'), type I' and the correct
spelling (e.g., I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.


I added this code in the cell: \rowcolor{cyan}, and I tried almost with this: \cellcolor{cyan}, but it doesn't work. I searched on the Internet some help about of how to set the color background in the cells, and some pages mentioned the package colortbl (but how can I use it, how does it works or what configuration in LyX do I need to change?)

I'll appreciate any help.

• Welcome to TeX.SX! It's probably because you haven't loaded the colortbl package, which defines the rowcolor command. To load the package, go to Document>Settings, scroll down the left panel to find LaTeX Preamble, and add the line \usepackage{colortbl}. Jun 11, 2014 at 22:37
• Thank you, dude, I'm new in TEX.SX and LaTeX. It works, but the lines of the cell are now of the color of the bakground cell (not black). How can fix it? Jun 11, 2014 at 22:56
• I'm not sure I follow what you're saying. Can you edit the question to make it clearer? A screenshot and some code snippet would be helpful as well. Jun 11, 2014 at 23:13
• @KevinC For example, I used \rowcolor to fill the header of lightgray, and I got this: link Jun 14, 2014 at 17:44

As Kevin has already pointed out in his comment: Load the colortbl package in the preamble.

In the case of LyX, it is generally a good idea to take a look into it's documentation before searching the internet: The necessary steps (including an example of using \rowcolor) are described precisely in Section 2.1.1 Colored Tables of the Embedded Objects Manual, which is available from the Help menu within LyX:

[...]

In LyX insert a

 \usepackage[table]{xcolor}


into Document->Preferences->Preamble (Preamble is on the lower left side)

In the first step add \usepackage{colortbl} to the LaTeX preamble. After that at the start of every row type this command:

\rowcolor[gray]{0.95}


For example:

\begin{tabular}{c c }
\rowcolor[gray]{0.95}  { a & b}\\
\rowcolor[gray]{0.95}  { c & d}\\
\end{tabular}
`

In many journals authors use gray color for tables.

• A full example would make this a full solution ;-)
– user31729
Aug 4, 2014 at 17:08