Graphics, Figures & Tables\cellcolor error? (colortbl package)

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
BtheK
Posts: 5
Joined: Mon Dec 14, 2009 5:02 pm

\cellcolor error? (colortbl package)

Post by BtheK »

Hi all,

In my report I want to use a table. Some cells of this table needs to be highlighted in different color, however the \cellcolor command does not work (while for example the \rowcolor does). Aneyone knows what I'm doing wrong?

Code:

documentclass[a4paper]{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{colortbl}

\begin{document}

\begin{table}[h!]
\centering
\begin{tabular}{llllll}
\toprule
& \multicolumn{ 2}{c}{{\bf Plan A}} & {\bf Plan B} & \multicolumn{ 2}{c}{{\bf Plan C}} \\
\hline
& {\bf Local} & {\bf Municipal} & {\bf Local} & {\bf Local} & {\bf Municipal} \\

\midrule

{\bf Min.} & {\bf Alternative Z4 } & Alternative X7 & \rowcolor[gray]{0.9} Alternative P1 & Alternative Q4 & \rowcolor[gray]{0.9} Alternative N3 \\

& $0.02 \pm 0.01$ & $0.04$ & \rowcolor[gray]{0.9} $0.02\pm 0.01$ & $0.004$ & \rowcolor[gray]{0.9} $0.0048$ \\

& $46 \pm 2.3 \%$ & $135 \%$ & \rowcolor[gray]{0.9} & $20 \%$ & \rowcolor[gray]{0.9} $41 \%$ \\

{\bf Max.} & Alternative V2 & Alternative V2 & Alternative K8 & \cellcolor[cmyk]{0.2,0,0,0} {\it Alternative G5 } & \cellcolor[cmyk]{0.2,0,0,0} {\it Alternative B3} \\

& $9.0 \pm 2.9$ & $1.41 \pm 0.05$ & $1.099$ & \cellcolor[cmyk]{0.2,0,0,0} $3.43 \pm 0.2$ & \cellcolor[cmyk]{0.2,0,0,0} $=3.83\pm 0.2$ \\

& $33.4\pm 6.5 \%$ & $=82.5\pm 9\%$ & $=0.080$ & \cellcolor[cmyk]{0.2,0,0,0} & \cellcolor[cmyk]{0.2,0,0,0} \\
\bottomrule
\end{tabular}
\end{table}

\end{document

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\cellcolor error? (colortbl package)

Post by gmedina »

Hi,

the \rowcolor command must be used at the start of a row, and in your code you are using it twice in the wrong position (not at the start of the row).

The following code works OK:

Code: Select all

\documentclass[a4paper]{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{colortbl}

\begin{document}

\begin{table}[!ht]
  \centering
  \begin{tabular}{llllll}
  \toprule
  & \multicolumn{ 2}{c}{{\bfseries Plan A}} & {\bfseries Plan B} & \multicolumn{ 2}{c}
    {{\bfseries Plan C}} \\
  \hline
  & {\bfseries Local} & {\bfseries Municipal} & {\bfseries Local} & {\bfseries Local} &   
    {\bfseries Municipal} \\
  \midrule
  {\bfseries Min.} & {\bfseries Alternative Z4 } & Alternative X7 & \cellcolor[gray]{0.9} 
    Alternative P1 & Alternative Q4 & \cellcolor[gray]{0.9} Alternative N3 \\
  & $0.02 \pm 0.01$ & $0.04$ & \cellcolor[gray]{0.9} $0.02\pm 0.01$ & $0.004$ & 
    \cellcolor[gray]{0.9} $0.0048$ \\
  & $46 \pm 2.3 \%$ & $135 \%$ & \cellcolor[gray]{0.9} & $20 \%$ & 
    \cellcolor[gray]{0.9} $41 \%$ \\
  {\bfseries Max.} & Alternative V2 & Alternative V2 & Alternative K8 & 
    \cellcolor[cmyk]{0.2,0,0,0} {\it Alternative G5 } & 
    \cellcolor[cmyk]{0.2,0,0,0} {\it Alternative B3} \\
  & $9.0 \pm 2.9$ & $1.41 \pm 0.05$ & $1.099$ & \cellcolor[cmyk]{0.2,0,0,0} $3.43 \pm 
    0.2$ & \cellcolor[cmyk]{0.2,0,0,0} $=3.83\pm 0.2$ \\
  & $33.4\pm 6.5 \%$ & $=82.5\pm 9\%$ & $=0.080$ & \cellcolor[cmyk]{0.2,0,0,0} & 
    \cellcolor[cmyk]{0.2,0,0,0} \\
  \bottomrule
  \end{tabular} 
\end{table}

\end{document}
By the way, \bf is an obsolete command and shouldn't be used anymore (refer to the l2tabu document for an explanation); you could use \bfseries or \textbf instead (again, refer to l2tabu).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
BtheK
Posts: 5
Joined: Mon Dec 14, 2009 5:02 pm

\cellcolor error? (colortbl package)

Post by BtheK »

Hi Gmedina,

First of all, thanks for your help. Unfortunately, the \rowcolor command is not the problem, that works fine. The \cellcolor command is the one which is not working.
If I run for example the code in your answer, I get the error: Undefined control sequence. 1.13 ...rnative Z4 } & Alternative X7 & \cellcolor [gray]{0.9} ?


Cheers, Bill

p.s. sorry for the \bf, I'll change it...
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\cellcolor error? (colortbl package)

Post by gmedina »

Well, the \rowcolor command will produce compilation errors if not used at the start of a row.

If you are getting errors with the exact code example that I posted, then I suspect that you might have old versions of some package(s). Let's consider that possibility first: add the command \listfiles before the \documentclass{article} line in my example code and compile your document. Now open the auxiliar .log file (it can be found in the same directory that contains the .tex file) and search for the section *File List* (this list was created by the \listfiles command).

Below, you can see the *File List* that I obtained after compilation of my example; compare it with yours to see if some packages are out of date. If so, please update your LaTeX system; if that's not the problem, then please attach to your next post the complete .log file.

Code: Select all

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
booktabs.sty    2005/04/14 v1.61803 publication quality tables
multirow.sty    
colortbl.sty    2001/02/13 v0.1j Color table columns (DPC)
   array.sty    2008/09/09 v2.4c Tabular extension package (FMi)
   color.sty    2005/11/14 v1.0j Standard LaTeX Color (DPC)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2009/08/25 v0.04m Graphics/color for pdfTeX
supp-pdf.mkii
 *********** 
1,1,2,3,5,8,13,21,34,55,89,144,233,...
BtheK
Posts: 5
Joined: Mon Dec 14, 2009 5:02 pm

Re: \cellcolor error? (colortbl package)

Post by BtheK »

Works fine now. Updating the colortbl package did the trick! Thanks a lot for your help

cheers, Bill
Post Reply