TransWikia.com

alternate row colors for only some columns

TeX - LaTeX Asked by DilithiumMatrix on February 1, 2021

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]  

3 Answers

You can get this effect by alternating row and column color like following:

newcolumntype{g}{>{columncolor{Gray}}c}
begin{table}[ht]
centering
begin{tabular}{c|g|g|g|g|g|g|g}
hline
rowcolor{white}
&col1 &col2 &col3 &col4 & col5 &col6 &col7
hline
rowcolor{white}
row1& ra & ra & ra & ra & ra & ra & ra 
row2& ra & ra & ra & ra & ra & ra & ra 
rowcolor{white}
row3& ra & ra & ra & ra & ra & ra & ra 
...
hline
end{tabular}
end{table}

Source: highlight table rows/columns

Answered by rkachach on February 1, 2021

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}

Answered by Zarko on February 1, 2021

In {NiceTabular} of nicematrix, you can programmatically color the rows, columns and cells as you want. Here, I color part of the rows with a cyle of length 4.

documentclass[6pt, oneside]{article}
usepackage{geometry}
geometry{letterpaper}
usepackage{graphicx}
usepackage{amssymb}
usepackage{booktabs}
usepackage{nicematrix}


ExplSyntaxOn
% The argument of myrowcolors is the starting row 
NewDocumentCommand myrowcolors { m }
  {
    int_step_inline:nnn { #1 } { arabic{iRow} } 
      {
        tl_set:Nx l_tmpa_int % the color
          {
            int_case:nn { int_mod:nn { ##1 - #1 + 1 } 4 }
              {
                1 { blue!05 }
                2 { blue!10 }
                3 { blue!20 }
                0 { blue!30 }
              }   
          }
        exp_args:NV rectanglecolor 
          l_tmpa_int { ##1 - 7} { ##1 - 10}
      }
  }
ExplSyntaxOff


usepackage{xcolor}
begin{document}
begin{table}
centering
renewcommand{arraystretch}{1.2}
setlength{tabcolsep}{4pt}
    begin{NiceTabular}{@{}ccccwc{7mm}wc{1cm}lccc}[code-before = myrowcolors{4}]
    toprule
        &                       & Block{1-4}{Background}&                   &               &               &
        
One     & Two   & Block{1-2}{Amp}&  & Block{1-2}{Spectrum} &  & Block{2-1}{Subset}  &  Stuff
        & Block{2-2}{Fraction}
        
Column  & Column    & $1$       & $0.1$ & $1$   & $0.1$                 
        &           & All (Coal)    & 
         midrule
Block{8-1}{Stuff}         
    & Block{4-1}{0.6}   & Block{4-1}{3.7}           & Block{4-1}{15}   &  Block{4-1}{-3}
    & Block{4-1}{-0.59}     
    &  All   &  29 (2.7)      &  $1875/9270$   
    &  $(0.20)$  
    &   &   &   &   &                     
    & $mu>0.2$  &  26 (2.2)  & $ 1225/4759$
    & $(0.26)$  
    &   &   &   &   &  
    &   $M>10^8$ &  17 (4.0)  &  $608/2610$
    &  $(0.23)$  
    &   &   &   &   &
    & $M>10^8$, $mu>0.2$    
                        & 6.9 (3.3)  & $214/478$  
    & $(0.45)$   
  & Block{4-1}{1.0}     & Block{4-1}{4.7}   & Block{4-1}{17}
  & Block{4-1}{-6}      & Block{4-1}{-0.38} 
  &  All     & 7.7 (1.2)      & $4634/9270$
  & $(0.50)$  
    &   &   &   &   &
    &               & 4.8 (1.0)     & $ 2900/4759$          
    & $(0.61)$      
    &   &   &   &   &  
    & $M>10^8$, & 4.9 (0.79)& $1422/2610$
    & $(0.54)$  
    &   &   &   &   &  
    & $M>10^8$, $mu>0.2$      & 0.35 (0.35) & $472/478$     & $(0.99)$  
    bottomrule
    end{NiceTabular}
caption{huh, finally my wonderful table :-)}
    label{tab:params}
end{table}
end{document}

Output of the above code

Answered by F. Pantigny on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP