无法为表格的正确行着色

无法为表格的正确行着色

我用表格生成器制作了一个表格。我希望前两行是白色的,接下来的一行是灰色和白色交替出现的。

这是我生成的代码:

序言:(因为这是一份很长的文件,所以很长)

    \documentclass[11pt]{report}
    \usepackage[utf8]{inputenc}
    \usepackage{helvet}
    \usepackage{xspace}
    \usepackage{subcaption}
    \usepackage{placeins}
    \usepackage{wrapfig}
    \usepackage{multirow}
    \usepackage[table,xcdraw]{xcolor}
    \usepackage{adjustbox} 
    \usepackage{booktabs,caption} 
    \usepackage[flushleft]{threeparttable}
    \usepackage{ragged2e}
    \usepackage{floatrow}
    \usepackage{graphicx,wrapfig,lipsum}
    \graphicspath{{../figs/}}
    \usepackage{pdfcomment}
    \usepackage{geometry}
    \newgeometry{left=2.5cm,right=2.5cm,bottom=2.5cm}
    \usepackage[english]{babel}
    \usepackage{CJKutf8}
    \pagestyle{headings}
    \usepackage{blindtext}
    \usepackage{csquotes}
    \usepackage{titlesec}
    \usepackage[T1]{fontenc}
    \usepackage{imakeidx}
    \makeindex[columns=3, title=Alphabetical Index]
    \usepackage{array}
    \usepackage{tikzpagenodes}
    \usepackage[format=plain, labelfont={bf,it}, textfont=it]{caption}
    \usepackage{fancyhdr}
    %\usepackage[toc,nopostdot, nonumberlist,style=long,automake,acronym]{glossaries}
    \usepackage{subfiles}
    \usepackage{hyperref}

The table in question:

    \begin{table}[hbt!]
    \caption{Crude prevalence rates of scurvy, rickets, tuberculosis and leprosy by sex and period for the extra-London London regions}\label{Table5.10}
    \begin{adjustbox}{max width=\columnwidth,center}
    \begin{tabular}{lllccccccccc}
    \textbf{Region} & \textbf{Period}           & \textbf{Sex} & \multicolumn{2}{c}{\textbf{Scurvy}} & \multicolumn{2}{c}{\textbf{Rickets}} & \multicolumn{2}{c}{\textbf{TB}} & \multicolumn{2}{c}{\textbf{Leprosy}} & \textbf{N} \\
    \textbf{}  & \textbf{} & \textbf{}  & \textbf{n} & \textbf{\%} & \textbf{n} & \textbf{\%} & \textbf{n} & \textbf{\%} & \textbf{n} & \textbf{\%} & \textbf{} \\ \hline
     & \textbf{Pre-Black Death}  & \cellcolor[HTML]{C0C0C0}\textbf{Female}  & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}1 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}4 & \cellcolor[HTML]{C0C0C0}1.69 & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}237 \\
     & \textbf{} & \textbf{Male} & 1 & & 3 & & 0 & & 2 & 0.01 & 253 \\
     & \textbf{} & \cellcolor[HTML]{C0C0C0}\textbf{Unknown} & \cellcolor[HTML]{C0C0C0}21 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}9 & \cellcolor[HTML]{C0C0C0}1.08 & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}832 \\
     & \textbf{Black Death}      & \textbf{Female} & 0 & & 0 & & 0 & & 0 & & 0 \\
     & \textbf{} & \cellcolor[HTML]{C0C0C0}\textbf{Male}    & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}0   \\
     & \textbf{} & \textbf{Unknown} & 0 & & 0 & & 0 & & 0 & & 0 \\
     & \textbf{Post-Black Death} & \cellcolor[HTML]{C0C0C0}\textbf{Female}  & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}5   \\
     & \textbf{} & \textbf{Male} & 0 & & 1 & 7.69 & 0 & & 0 & & 13 \\
     & \textbf{} & \cellcolor[HTML]{C0C0C0}\textbf{Unknown} & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}1   \\
    \multirow{-10}{*}{\textbf{Extra London}} & \multicolumn{2}{l}{\textbf{Subtotal}}  & 22 & 1.64 & 14 & 1.04 & 4 & 0.003 & 2 & 0.001 & 1341 \\ \hline
     & \textbf{Pre-Black Death}  & \cellcolor[HTML]{C0C0C0}\textbf{Female}  & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}9   \\
     & \textbf{} & \textbf{Male} & 0 & & 0 & & 0 & & 0 & & 38 \\
     & \textbf{} & \cellcolor[HTML]{C0C0C0}\textbf{Unknown} & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}27  \\
     & \textbf{Black Death}      & \textbf{Female} & 0 & & 0 & & 0 & & 0 & & 54 \\
     & \textbf{} & \cellcolor[HTML]{C0C0C0}\textbf{Male}    & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}104 \\
     & \textbf{} & \textbf{Unknown} & 0 & & 1 & 0.42 & 2 & 0.01 & 0 & & 240 \\
     & \textbf{Post-Black Death} & \cellcolor[HTML]{C0C0C0}\textbf{Female}  & \cellcolor[HTML]{C0C0C0}0  & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}1 & \cellcolor[HTML]{C0C0C0}2.7  & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0}     & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}37  \\
     & \textbf{} & \textbf{Male} & 0 & & 1 & 1.69 & 1 & 0.02 & 0 & & 59 \\
     & \textbf{} & \cellcolor[HTML]{C0C0C0}\textbf{Unknown} & \cellcolor[HTML]{C0C0C0}3  & \cellcolor[HTML]{C0C0C0}2.63 & \cellcolor[HTML]{C0C0C0}1 & \cellcolor[HTML]{C0C0C0}0.88 & \cellcolor[HTML]{C0C0C0}2 & \cellcolor[HTML]{C0C0C0}0.02 & \cellcolor[HTML]{C0C0C0}0 & \cellcolor[HTML]{C0C0C0} & \cellcolor[HTML]{C0C0C0}114 \\
    \multirow{-10}{*}{\textbf{London}}       & \multicolumn{2}{l}{\textbf{Subtotal}}  & 3 & 0.44 & 4 & 0.59 & 5 & 0.01 & 0 & & 682 \\ \hline
    {\color[HTML]{333333} \textbf{Total}}    & &  & 25 & 1.24 & 18 & 0.89 & 9 & 0.004 & 2 & 0.001 & 2023 
    \\hline
    \multicolumn{12}{l}{\footnotesize{\textsuperscript{1.}{T=Tuberculosis}}} 
    \end{tabular}
    \end{adjustbox}
    \end{table} 

问题是颜色开始的时间比我希望的早了一行。带有 n 和 % 符号的第二行应该是白色的,倒数第二行也应该是彩色的。我不明白为什么会发生这种情况。我看不到任何代码表明第二行是彩色的。

我还收到一个错误:

\multicolumn{12}{l}{\footnotesize{\textsuperscript{1.}{T=Tuberculosis}}} 这句话是misplaced \omit……

答案1

我不太清楚你到底想要什么。

这是一个{NiceTabular*}使用 的解决方案nicematrix

\documentclass[11pt]{report}
\usepackage{geometry}
\newgeometry{left=2.5cm,right=2.5cm,bottom=2.5cm}
\usepackage[english]{babel}
\usepackage[format=plain, labelfont={bf,it}, textfont=it]{caption}
\usepackage{nicematrix}
\usepackage{booktabs} 
\usepackage{hyperref}

\begin{document}

\begin{table}[hbt!]
\caption{Crude prevalence rates of scurvy, rickets, tuberculosis and leprosy by sex and period for the extra-London London regions}\label{Table5.10}

\setlength{\tabcolsep}{5pt}
\begin{NiceTabular*}{\linewidth}{@{}l@{\extracolsep{\fill}}llccccccccr@{}}[tabularnote = {T=Tuberculosis}]
\CodeBefore
\rowcolors{3-22}{lightgray}{}[cols=4-*]
\Body
\toprule
\RowStyle[bold]{}
Region & Period & Sex & \Block{1-2}{Scurvy} && \Block{1-2}{Rickets} && \Block{1-2}{TB} && \Block{1-2}{Leprosy} && N \\
 & &  & n & \% & n & \% & n & \% & n & \% & \\ 
\midrule
Extra London
 & Pre-Black Death & Female & 0 & & 1 & & 4 & 1.69 & 0 & & 237 \\
 & & Male & 1 & & 3 & & 0 & & 2 & 0.01 & 253 \\
 & & Unknown & 21 & & 9 & 1.08 & 0 & & 0 & & 832 \\
 & Black Death & Female & 0 & & 0 & & 0 & & 0 & & 0 \\
 & & Male & 0 & & 0 & & 0 & & 0 & & 0 \\
 & & Unknown & 0 & & 0 & & 0 & & 0 & & 0 \\
 & Post-Black Death & Female & 0 & & 0 & & 0 & & 0 & & 5 \\
 & & Male & 0 & & 1 & 7.69 & 0 & & 0 & & 13 \\
 & & Unknown & 0 & & 0 & & 0 & & 0 & & 1 \\
 & \Block{1-2}{Subtotal} && 22 & 1.64 & 14 & 1.04 & 4 & 0.003 & 2 & 0.001 & 1341 \\ 
\midrule
London
 & Pre-Black Death & Female & 0 & & 0 & & 0 & & 0 & & 9 \\
 & & Male & 0 & & 0 & & 0 & & 0 & & 38 \\
 & & Unknown & 0 & & 0 & & 0 & & 0 & & 27 \\
 & Black Death & Female & 0 & & 0 & & 0 & & 0 & & 54 \\
 & & Male & 0 & & 0 & & 0 & & 0 & & 104 \\
 & & Unknown & 0 & & 1 & 0.42 & 2 & 0.01 & 0 & & 240 \\
 & Post-Black Death & Female & 0 & & 1 & 2.7 & 0 & & 0 & & 37 \\
 & & Male & 0 & & 1 & 1.69 & 1 & 0.02 & 0 & & 59 \\
 & & Unknown & 3 & 2.63 & 1 & 0.88 & 2 & 0.02 & 0 & & 114 \\
 & \Block{1-2}{Subtotal} && 3 & 0.44 & 4 & 0.59 & 5 & 0.01 & 0 & & 682 \\ 
\midrule
 Total & & & 25 & 1.24 & 18 & 0.89 & 9 & 0.004 & 2 & 0.001 & 2023 \\ 
\bottomrule
\end{NiceTabular*}
\end{table}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容