1

Would anyone know how to add color in selected columns for a table with subcolumns?

In other words, for the image below, I would like to color in the 2nd and 3rd columns (clustered and cured OR & P-value) as well as the 6th and 7th column (Non-clustered and Cured OR & P-value). I tried using

rowcolor{black}\multicolumn{9}{}

but the attempts I tried from other help pages did not seem to help. Any help or hints would be greatly appreciated again. Thanks!

enter image description here

\documentclass[table]{beamer}

\usepackage{tikz, dcolumn, booktabs,multirow, esvect, xcolor}
\usepackage[utf8]{inputenc}
\usepackage{algorithm,algpseudocode}
\usepackage{cite}
\usetikzlibrary{arrows,shapes}
\newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type
\renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\newcommand\mcn[2]{\multicolumn{#1}{c}{#2}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}
\begin{document}
\begin{frame}

\begin{table}
\setlength\tabcolsep{0pt}
    \small{\caption{Model risk comparisons }}
\tiny
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
                      >{$}r<{$}
                      d{2.8} d{2.3}
                      d{2.3} d{1.0}
                      d{1.3} d{1.0}
                      d{3.0} d{1.0}
                      }
\toprule
& \mcn{4}{Clustered}                & \mcn{4}{Non-clustered}        \\
\cmidrule(lr){2-5}\cmidrule(lr){6-9}
 Logistic   & \mcn{2}{Cured}    & \mcn{2}{Non-cured}
        & \mcn{2}{Cured}        & \mcn{2}{Non-cured}                \\
& \mc{OR (95\% CI)}   & \mc{P-val}
    & \mc{OR (95\% CI)}   & \mc{P-val}    & \mc{OR (95\% CI)}
        & \mc{P-val}  & \mc{OR (95\% CI)}   & \mc{P-val}      \\
\midrule
\multicolumn{8}{l}{HCV Groupings \textsuperscript{a}}                              \\
D-/R+    & 0.86         & 0.36 &  -  & - &  0.37     & 0.99 & -   & - \\
        & (0.62, 1.19)    &        &        & &   (0.0,\infty)     & &    &\\
D+/R-    & 0.01         & <0.0001 \ast&  -  & - &  0.03    & 0.98 & -   & - \\
        & (0.01, 0.02)    &        &        & &   (0.0, \infty)     & &    &\\
D+/R+    & 0.71        & 0.03 &  -  & - &  0.31     & 0.99 & -   & - \\
        & (0.52, 0.97)    &        &        & &   (0.0, \infty)     & &    &\\
\text{Intercept}    & 0.22         & <0.0001 \ast&  -  & - &  1.96\cdot10^{-7}     & 0.63 & -  & - \\
        & (0.19, 0.26)    &        &        & &   (2.01 \cdot10^{-34}, 1.90\cdot10^{20})     & &    &\\
\midrule

   Survival & \mc{HR (95\% CI)}   & \mc{P-val}
    & \mc{HR (95\% CI)}   & \mc{P-val}    & \mc{HR (95\% CI)}
        & \mc{P-val}  & \mc{HR (95\% CI)}   & \mc{P-val}      \\
   \midrule
\multicolumn{8}{l}{HCV Groupings \textsuperscript{a}}                              \\
D-/R+       & 1.56      & <0.0001\ast & 1.59 &  <0.0001 \ast& 1.57  & <0.0001 \ast& 1.57 & <0.0001\ast  \\
        & (1.52, 1.61)    &         & (1.55, 1.64)       & & (1.52, 1.61)       & &  (1.53, 1.61)  &       \\
D+/R-       & 1.63      & <0.0001\ast & 1.74 & <0.0001\ast& 1.71  & <0.0001\ast & 1.71 & <0.0001 \ast  \\
        & (1.60, 1.66)    &         & (1.72, 1.77)& & (1.69, 1.73)   & & (1.69, 1.73)   &       \\
D+/R+       & 1.31     & <0.0001 \ast& 1.34 & <0.0001\ast& 1.34  & <0.0001\ast & 1.35 & <0.0001\ast  \\
        & (1.28, 1.35)    &         & (1.30, 1.37)       & & (1.32, 1.38)       & & (1.32, 1.38)   &       \\
%\text{Constant}    & 8.79         & <0.0001 \ast&  8.80  & <0.0001\ast &  8.71     & <0.0001\ast & 8.71   & <0.0001\ast \\
%            & (0.03)    &        &  (0.03)      & &   (0.004)     & & (0.004)   &\\
\midrule
& \mc{$\hat{\beta}$ (SD)}   & \mc{P-val}
    & \mc{$\hat{\beta}$ (SD)}   & \mc{P-val}    & \mc{$\hat{\beta}$ (SD)}
        & \mc{P-val}  & \mc{$\hat{\beta}$ (SD)}   & \mc{P-val}      \\
\midrule
\text{Center}    & -3.11         & <0.0001 \ast&  -  & - &  -     & - & -   & - \\
        & (-3.27, -2.96)    &        &        & &        & &    &\\
\text{theta}& 0.44      & <0.0001\ast &  0.24      & <0.0001\ast& -      & - &  -  &       - \\
        & (0.42, 0.45)    &         &  (0.22, 0.25)      & &        & &    &       \\
\bottomrule
\end{tabular*}

\end{table} 
\tiny{\textsuperscript{a} D-/R- as base group}


  \end{frame}
 \end{document}

1 Answer 1

1

for coloring of columns is intended macro from colortbl package (loaded by beamer): >{\columncolor{gray!10}}d{...}, but before you should solve more issue in table design and appearing of coloring in column. i sugest to solve this step by step.

1. step:

  • reformat table in which omit use of @{\extracolsep{\fill}}
  • determine size of tabcolsep
  • reformat size of columns (that contents not spill out of cell border)
  • increase space for table with macro adjustwidth from package changepage
  • replace \begin{tabular*}{\textwidth}{...} with \begin{tabular}{...}

2. step:

  • define own color name with \colorlet{mycol}{gray!10} (you can change color name) for coloring column with this the colors in columns (and cells) is central controlled ans easily changeable
  • define new commands for coloring local column heading: \newcommand\mcc[1]{\multicolumn{1}{c}{\cellcolor{mycol}{#1}}}
  • replace commands \mc{...} with \mcc{...} accordingly (in colored columns)

enter image description here

\documentclass[table]{beamer}
\colorlet{mycol}{gray!10}

\usepackage{tikz, dcolumn, booktabs,multirow, esvect, xcolor}
\usepackage[utf8]{inputenc}
\usepackage{algorithm,algpseudocode}
\usepackage{cite}
\usetikzlibrary{arrows,shapes}

\newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type, corrected 
\renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"
\newcommand{\ra}[2]{\renewcommand{\arraystretch}{#1}}

\newcommand\mcn[2]{\multicolumn{#1}{c}{#2}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}% corrected  
\newcommand\mcc[1]{\multicolumn{1}{c}{\cellcolor{mycol}{#1}}}
\usepackage{changepage}

\begin{document}
\begin{frame}

\begin{table}
\begin{adjustwidth}{-9mm}{-9mm}
\setlength\tabcolsep{1pt}
\tiny
\centering
    \caption{Model risk comparisons}
\begin{tabular}{
          >{$}l<{$}
         >{\columncolor{mycol}}d{4.9} >{\columncolor{mycol}}d{4.5}
          d{2.8} d{4.5}
          >{\columncolor{mycol}}d{2.18} >{\columncolor{mycol}}d{4.5}
          d{2.8} d{4.5}
                }
\toprule
& \mcn{4}{Clustered}                & \mcn{4}{Non-clustered}                \\
\cmidrule(lr){2-5}\cmidrule(lr){6-9}
 Logistic   & \mcn{2}{\cellcolor{gray!10}{Cured}}    & \mcn{2}{Non-cured}
        & \mcn{2}{\cellcolor{gray!10}{Cured}}        & \mcn{2}{Non-cured}   \\
& \mcc{OR (95\% CI)}   & \mcc{P-val}
    & \mc{OR (95\% CI)}   & \mc{P-val}    & \mcc{OR (95\% CI)}
        & \mcc{P-val}  & \mc{OR (95\% CI)}    & \mc{P-val}\\
\midrule
\multicolumn{8}{l}{HCV Groupings \textsuperscript{a}}                       \\
D-/R+   & 0.86         & 0.36 &  -  & - & 0.37          & 0.99  & - & -     \\
        & (0.62, 1.19) &      &     &   & (0.0,\infty)  &       &   &       \\
D+/R-   & 0.01         & <0.0001 \ast&  -  & - &  0.03  & 0.98  & - & -     \\
        & (0.01, 0.02) &      &     &   & (0.0, \infty) &       &   &       \\
D+/R+   & 0.71         & 0.03 &  -  & - &  0.31         & 0.99  & - & -     \\
        & (0.52, 0.97) &      &     &   & (0.0, \infty) &       &   &       \\
\text{Intercept}
        & 0.22         & <0.0001 \ast& - & - & 1.96\cdot10^{-7} & 0.63 & -  & - \\
        & (0.19, 0.26) &      &     &   & (2.01 \cdot10^{-34}, 1.90\cdot10^{20})
                                                        &       &    &      \\
\midrule
   Survival & \mcc{HR (95\% CI)}  & \mcc{P-val}
    & \mc{HR (95\% CI)}   & \mc{P-val}    & \mcc{HR (95\% CI)}
        & \mcc{P-val} & \mc{HR (95\% CI)}   & \mc{P-val}\\
   \midrule
\multicolumn{8}{l}{HCV Groupings \textsuperscript{a}}                              \\
D-/R+   & 1.56        & <0.0001\ast & 1.59 &  <0.0001 \ast& 1.57  & <0.0001 \ast& 1.57 & <0.0001\ast  \\
        & (1.52, 1.61)    &         & (1.55, 1.64)       & & (1.52, 1.61)       & &  (1.53, 1.61)  &       \\
D+/R-   & 1.63      & <0.0001\ast & 1.74 & <0.0001\ast& 1.71  & <0.0001\ast & 1.71 & <0.0001 \ast  \\
        & (1.60, 1.66)    &         & (1.72, 1.77)& & (1.69, 1.73)   & & (1.69, 1.73)   &       \\
D+/R+   & 1.31     & <0.0001 \ast& 1.34 & <0.0001\ast& 1.34  & <0.0001\ast & 1.35 & <0.0001\ast  \\
        & (1.28, 1.35)    &         & (1.30, 1.37)       & & (1.32, 1.38)       & & (1.32, 1.38)   &       \\
%\text{Constant}    & 8.79         & <0.0001 \ast&  8.80  & <0.0001\ast &  8.71     & <0.0001\ast & 8.71   & <0.0001\ast \\
%            & (0.03)    &        &  (0.03)      & &   (0.004)     & & (0.004)   &\\
\midrule
& \mcc{$\hat{\beta}$ (SD)}  & \mcc{P-val}
    & \mc{$\hat{\beta}$ (SD)}   & \mc{P-val}   & \mcc{$\hat{\beta}$ (SD)}
        & \mcc{P-val} & \mc{$\hat{\beta}$ (SD)}   & \mc{P-val}                  \\
\midrule
\text{Center}
        & -3.11         & <0.0001 \ast  & - & - &  -    & - & - & -             \\
        & (-3.27, -2.96)&               &   &   &       &   &   &               \\
\text{theta}& 0.44      & <0.0001\ast &  0.24      & <0.0001\ast& - & - & - & - \\
        & (0.42, 0.45)  &             &  (0.22, 0.25)    &  &   &   &    &      \\
\midrule[\heavyrulewidth]
\multicolumn{8}{l}{\textsuperscript{a} D-/R- as base group}
\end{tabular}
\end{adjustwidth}
\end{table}
\end{frame}
\end{document}

3. step:

  • in case, that you like to remove white space above and below horizontal rules, then you had to add into preamble

\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}

and after \setlength\tabcolsep{1pt}

   \renewcommand\arraystretch{1.3}

after this changes you will obtain:

enter image description here

5
  • @rich, any news?
    – Zarko
    Commented Sep 3, 2017 at 13:02
  • This is what I was looking for! Thanks. I think it was the creation of two multicolumn - one with and without cellcolor command that was throwing everything off for me before as well as the tabular* and tabular distinction. Anyways, thank you so much!
    – rich
    Commented Sep 5, 2017 at 14:42
  • did the code above compile correctly for you? It seems that there is a problem with the \mc when I made the adjustments as suggested, @Zarko?
    – rich
    Commented Sep 6, 2017 at 13:34
  • @rich, ups ... i find now, that i didn't copy recent version of my code and that in the provided are missing last two lines :-(. corrected now. i also add third step in this table (re)design. maybe you like it :)
    – Zarko
    Commented Sep 6, 2017 at 14:16
  • yeah, the new code/ design works well =) Fiddling around with the table size/design can be quite confusing in my opinion compounded with the multicolumns, @Zarko. Anyways, thanks for your help!
    – rich
    Commented Sep 7, 2017 at 1:05

You must log in to answer this question.

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