对于包含子列的多列表格,每隔一列添加颜色

对于包含子列的多列表格,每隔一列添加颜色

有人知道如何为带有子列的表的选定列添加颜色吗?

换句话说,对于下图,我想为第 2 列和第 3 列(聚集和治愈的 OR 和 P 值)以及第 6 列和第 7 列(非聚集和治愈的 OR 和 P 值)着色。我尝试使用

rowcolor{black}\multicolumn{9}{}

但我尝试过的其他帮助页面似乎没有帮助。任何帮助或提示都将不胜感激。谢谢!

在此处输入图片描述

\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

列的颜色预期使用来自colortbl包(由 beamer 加载)的宏:>{\columncolor{gray!10}}d{...},但在此之前您应该解决表格设计和列颜色出现中的更多问题。我建议逐步解决这个问题。

1.步骤:

  • 重新格式化表格,其中省略使用@{\extracolsep{\fill}}
  • 确定大小tabcolsep
  • 重新格式化列的大小(内容不会溢出单元格边框)
  • adjustwidth使用包中的宏增加表格空间changepage
  • \begin{tabular*}{\textwidth}{...}用。。。来代替\begin{tabular}{...}

第二步:

  • 为着色列定义自己的颜色名称\colorlet{mycol}{gray!10}(您可以更改颜色名称),这样列(和单元格)中的颜色就可以集中控制,并且可以轻松更改
  • 定义用于为本地列标题着色的新命令:\newcommand\mcc[1]{\multicolumn{1}{c}{\cellcolor{mycol}{#1}}}
  • \mc{...}用相应的命令替换\mcc{...}(在彩色列中)

在此处输入图片描述

\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}

第三步:

  • 如果你想删除水平线上方和下方的空白,那么你必须添加到序言中

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

之后\setlength\tabcolsep{1pt}

   \renewcommand\arraystretch{1.3}

经过这些改变你将获得:

在此处输入图片描述

相关内容