[Tex/LaTex] alternate row colors for only some columns

colorformattingtables

I'd like alternating row colors for only specific columns (and in a perfect world, also only a subset of rows), with a table using multirow and multicolumn. I've found that putting \rowcolor{COLOR} starting with the target column works (shown below)… but it raises a misplaced \noalign error.

I'm using TexShop (3.6.1) on OSX, with pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015)

Is there a better way to do this? Or a way to get the above method to not raise an error?

enter image description here

[![\documentclass\[6pt, oneside\]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage\[cm\]{fullpage}

\usepackage\[table\]{xcolor}

\newcommand{\mc}\[2\]{\multicolumn{#1}{c}{#2}}
\newcommand{\mr}\[2\]{\multirow{#1}{*}{#2}}


\begin{document}


    \begin{table}\centering
    \renewcommand{\arraystretch}{1.2}
    \setlength{\tabcolsep}{4pt}
    \begin{tabular}{@{}ccc ccc lccc@{}}
    \toprule
                            &                   & \mc{4}{Background}                                        &                   &               &               &           \\
    \mr{2}{One Column}      & \mr{2}{Two Column}& \mc{2}{Amp}                   & \mc{2}{Spectrum}          & \mr{2}{Subset}    & Stuff         & \mc{2}{\mr{2}{Fraction}}  \\ 
                            &                   & $1$           & $0.1$         & $1$       & $0.1$         &                   & All (Coal)    & \mc{2}{}                  \\\midrule
    \mr{8}{Stuff}           & \mr{4}{0.6}       & \mr{4}{3.7}   & \mr{4}{15}    & \mr{4}{-3}& \mr{4}{-0.59} & \rowcolor{blue!20}All             & 29  (2.7)     & $ 1875/ 9270$ & $(0.20)$  \\
                            &                   &               &               &           &               & \rowcolor{blue!30}$\mu>0.2$       & 26  (2.2)     & $ 1225/ 4759$ & $(0.26)$  \\
                            &                   &               &               &           &               & \rowcolor{blue!20}$M>10^8$        & 17  (4.0)     & $ 608 / 2610$ & $(0.23)$  \\
                            &                   &               &               &           &               & \rowcolor{blue!30}$M>10^8$,$\mu>0.2$& 6.9 (3.3)   & $ 214 /  478$ & $(0.45)$  \\

                            & \mr{4}{1.0}       & \mr{4}{4.7}   & \mr{4}{17}    & \mr{4}{-6}& \mr{4}{-0.38} & \rowcolor{blue!10}All             & 7.7 (1.2)     & $ 4634/ 9270$ & $(0.50)$  \\
                            &                   &               &               &           &               & \rowcolor{blue!20}$\mu>0.2$       & 4.8 (1.0)     & $ 2900/ 4759$ & $(0.61)$  \\
                            &                   &               &               &           &               & \rowcolor{blue!10}$M>10^8$,       & 4.9 (0.79)    & $ 1422/ 2610$ & $(0.54)$  \\
                            &                   &               &               &           &               & \rowcolor{blue!20}$M>10^8$,$\mu>0.2$& 0.35 (0.35) & $  472/  478$ & $(0.99)$  \\

    \bottomrule
    \end{tabular}
    \caption{}
    \label{tab:params}
    \end{table}



\end{document}][1]][1]  

Best Answer

enter image description here

To obtain above image I first clean-up your code. It contain bunch of errors caused by wrong use options (they had to be inside [...] and not in math environment \[...\]). Then to each cell, which you like to have colored, wrote \cellcolor{<selected color>}. This is only way (for which I know) that you have only part row in desired color. To simplify this task (since all colors ara based on blue), I defined new command:

\newcommand{\CC}[1]{\cellcolor{blue!#1}}

and (after lot of work) I rewrote your MWE into:

\documentclass[6pt, oneside]{article}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{tabularx}
%\usepackage[cm]{fullpage}

\usepackage[table]{xcolor}
\newcommand{\mc}[2]{\multicolumn{#1}{c}{#2}}
\newcommand{\mr}[2]{\multirow{#1}{*}{#2}}
\newcommand{\CC}[1]{\cellcolor{blue!#1}}
    \begin{document}
\begin{table}
    \centering
\renewcommand{\arraystretch}{1.2}
\setlength{\tabcolsep}{4pt}
    \begin{tabular}{@{}*{6}{c} lccc}
    \toprule
        &                       & \mc{4}{Background}&                   &               &               &
        \\
One     & Two   & \mc{2}{Amp}   & \mc{2}{Spectrum}  & \mr{2}{Subset}    & Stuff
        & \mc{2}{\mr{2}{Fraction}}
        \\
Column  & Column    & $1$       & $0.1$ & $1$   & $0.1$                 
        &           & All (Coal)    & \mc{2}{}                  
        \\ \midrule
\mr{8}{Stuff}         
    & \mr{4}{0.6}   & \mr{4}{3.7}           & \mr{4}{15}   &  \mr{4}{-3}
    & \mr{4}{-0.59}     
    & \CC{20} All   & \CC{20} 29 (2.7)      & \CC{20} $1875/9270$   
    & \CC{20} $(0.20)$  \\
    &   &   &   &   &                     
    & \CC{30}$\mu>0.2$  & \CC{30} 26 (2.2)  & \CC{30}$ 1225/4759$
    & \CC{30}$(0.26)$  \\
    &   &   &   &   &  
    & \CC{20}  $M>10^8$ & \CC{20} 17 (4.0)  & \CC{20} $608/2610$
    & \CC{20} $(0.23)$  \\
    &   &   &   &   &
    & \CC{30}$M>10^8$, $\mu>0.2$    
                        & \CC{30}6.9 (3.3)  & \CC{30}$214/478$  
    & \CC{30}$(0.45)$   \\
  & \mr{4}{1.0}     & \mr{4}{4.7}   & \mr{4}{17}
  & \mr{4}{-6}      & \mr{4}{-0.38} 
  & \CC{10} All     & \CC{10}7.7 (1.2)      & \CC{10}$4634/9270$
  & \CC{10}$(0.50)$  \\
    &   &   &   &   &
    &               & 4.8 (1.0)     & $ 2900/4759$          
    & $(0.61)$      \\
    &   &   &   &   &  
    & \CC{10}$M>10^8$, & \CC{10}4.9 (0.79)& \CC{10}$1422/2610$
    & \CC{10}$(0.54)$  \\
    &   &   &   &   &  
    & \CC{20}$M>10^8$,$\mu>0.2$      & \CC{20}0.35 (0.35) & \CC{20}$472/478$     & \CC{20}$(0.99)$  \\
    \bottomrule
    \end{tabular}
\caption{huh, finally my wonderful table :-)}
    \label{tab:params}
\end{table}
    \end{document}
Related Question