无法正确格式化表格(\hline 和彩色单元格)

无法正确格式化表格(\hline 和彩色单元格)

我使用在线表格转换器网站创建了此表格,然后对其进行了调整,直到得到我想要的结果。我使用 overleaf 编译所有内容。

\begin{table}[h!]
\caption{Composition of the study sample in the pre-Black Death period by site.}\label{Table5.2}
\begin{tabular}{@{}llccccc@{}}
\toprule
\textbf{Region}& \textbf{Site}& \multicolumn{2}{c}{\textbf{Early pre-}}& \multicolumn{2}{c}{\textbf{Late pre-}}& \textbf{Total}\\
\textbf{}& \textbf{}& \multicolumn{2}{c}{\textbf{Black Death}}& \multicolumn{2}{c}{\textbf{Black Death}}& \textbf{}\\
\textbf{}& \textbf{}& \textbf{n}& \textbf{\%}& \textbf{n}& \textbf{\%}& \textbf{N}\\ \hline
\textbf{Extra-London} & \cellcolor[HTML]{E7E6E6}\textbf{Wharram Percy}& \cellcolor[HTML]{E7E6E6}7& \cellcolor[HTML]{E7E6E6}12.07 & \cellcolor[HTML]{E7E6E6}51& \cellcolor[HTML]{E7E6E6}87.93 & \cellcolor[HTML]{E7E6E6}58\\
\textbf{}& \textbf{St Oswald's}& 44& 29.33 & 106 & 70.67 & 150 \\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{St Anne's Charterhouse} & \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}& \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}& \cellcolor[HTML]{E7E6E6}0\\
\textbf{}& \textbf{Barton Upon Humber}& 197 & 66.33 & 100 & 33.67 & 297 \\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{Stoke Quay}& \cellcolor[HTML]{E7E6E6}191 & \cellcolor[HTML]{E7E6E6}35.97 & \cellcolor[HTML]{E7E6E6}340 & \cellcolor[HTML]{E7E6E6}64.03 & \cellcolor[HTML]{E7E6E6}531 \\ \cline{2-7} 
\textbf{}& \textbf{Subtotal}& 439 & 42.37 & 597 & 57.63 & 1036 \\
\midrule
\textbf{London}& \cellcolor[HTML]{E7E6E6}\textbf{Guildhall Yard}& \cellcolor[HTML]{E7E6E6}10& \cellcolor[HTML]{E7E6E6}27.03 & \cellcolor[HTML]{E7E6E6}27& \cellcolor[HTML]{E7E6E6}72.97 & \cellcolor[HTML]{E7E6E6}37\\
\textbf{}& \textbf{East Smithfield}& 0&& 0&& 0\\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{Merton Priory}& \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}37& \cellcolor[HTML]{E7E6E6}100& \cellcolor[HTML]{E7E6E6}37\\
\textbf{}& \textbf{St Mary Graces}& 0&& 0&& 0\\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{Spital Square} & \cellcolor[HTML]{E7E6E6}0 & \cellcolor[HTML]{E7E6E6}& \cellcolor[HTML]{E7E6E6}40& \cellcolor[HTML]{E7E6E6} 100 & \cellcolor[HTML]{E7E6E6}40\\ \cline{2-7} 
\textbf{}& \textbf{Subtotal}& 10& 8.77& 104 & 91.23 & 114 \\ \hline
\textbf{Total} & & 449 & 39.04 & 701 & 60.96 & 1150\\
\bottomrule
\end{tabular}
\end{table}

表格看起来很乱,但可以编译。我遇到的问题是,编译后,表格如下所示:

表格显示了上述代码的编译方式

它包含了我想要的所有信息,但我有几个问题:

  1. 除了伦敦外层空间顶部的第二条线外,所有线都正确。它应该横跨整个图表,但它只覆盖了一个单元格。
  2. 中线下方有一个小的白色间隙(以伦敦开头的间隙)。我经常看到这种情况,不知道如何去除它。
  3. 彩色单元格延伸到线条和边距之外。我希望线条和单元格在页面边距末端具有相同的限制。
  4. 我不确定,但看起来所有单元格的高度都不一样,第二个小计看起来比第一个短(我可能错了)。

我该如何解决这些问题?欢迎提出任何建议。

非常感谢,

答案1

Mico 的回答可能是好的答案(+1),但如果你真的想在你的问题中实现你心中的设计,你可以{NiceTabular}使用nicematrix

在下面的例子中,我通过替换(使用键)来使用您{tabular}{NiceTabular}代码colortbl-like

\documentclass{article}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{geometry}
\usepackage{nicematrix}

\begin{document}
\begin{table}[h!]
\caption{Composition of the study sample in the pre-Black Death period by site.}\label{Table5.2}
\begin{NiceTabular}{@{}llccccc@{}}[colortbl-like]
\toprule
\textbf{Region}& \textbf{Site}& \multicolumn{2}{c}{\textbf{Early pre-}}& \multicolumn{2}{c}{\textbf{Late pre-}}& \textbf{Total}\\
\textbf{}& \textbf{}& \multicolumn{2}{c}{\textbf{Black Death}}& \multicolumn{2}{c}{\textbf{Black Death}}& \textbf{}\\
\textbf{}& \textbf{}& \textbf{n}& \textbf{\%}& \textbf{n}& \textbf{\%}& \textbf{N}\\ \hline
\textbf{Extra-London} & \cellcolor[HTML]{E7E6E6}\textbf{Wharram Percy}& \cellcolor[HTML]{E7E6E6}7& \cellcolor[HTML]{E7E6E6}12.07 & \cellcolor[HTML]{E7E6E6}51& \cellcolor[HTML]{E7E6E6}87.93 & \cellcolor[HTML]{E7E6E6}58\\
\textbf{}& \textbf{St Oswald's}& 44& 29.33 & 106 & 70.67 & 150 \\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{St Anne's Charterhouse} & \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}& \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}& \cellcolor[HTML]{E7E6E6}0\\
\textbf{}& \textbf{Barton Upon Humber}& 197 & 66.33 & 100 & 33.67 & 297 \\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{Stoke Quay}& \cellcolor[HTML]{E7E6E6}191 & \cellcolor[HTML]{E7E6E6}35.97 & \cellcolor[HTML]{E7E6E6}340 & \cellcolor[HTML]{E7E6E6}64.03 & \cellcolor[HTML]{E7E6E6}531 \\ \cline{2-7} 
\textbf{}& \textbf{Subtotal}& 439 & 42.37 & 597 & 57.63 & 1036 \\
\midrule
\textbf{London}& \cellcolor[HTML]{E7E6E6}\textbf{Guildhall Yard}& \cellcolor[HTML]{E7E6E6}10& \cellcolor[HTML]{E7E6E6}27.03 & \cellcolor[HTML]{E7E6E6}27& \cellcolor[HTML]{E7E6E6}72.97 & \cellcolor[HTML]{E7E6E6}37\\
\textbf{}& \textbf{East Smithfield}& 0&& 0&& 0\\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{Merton Priory}& \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}0& \cellcolor[HTML]{E7E6E6}37& \cellcolor[HTML]{E7E6E6}100& \cellcolor[HTML]{E7E6E6}37\\
\textbf{}& \textbf{St Mary Graces}& 0&& 0&& 0\\
\textbf{}& \cellcolor[HTML]{E7E6E6}\textbf{Spital Square} & \cellcolor[HTML]{E7E6E6}0 & \cellcolor[HTML]{E7E6E6}& \cellcolor[HTML]{E7E6E6}40& \cellcolor[HTML]{E7E6E6} 100 & \cellcolor[HTML]{E7E6E6}40\\ \cline{2-7} 
\textbf{}& \textbf{Subtotal}& 10& 8.77& 104 & 91.23 & 114 \\ \hline
\textbf{Total} & & 449 & 39.04 & 701 & 60.96 & 1150\\
\bottomrule
\end{NiceTabular}
\end{table}
\end{document}

但是,您需要进行多次编译(因为nicematrix在后台使用了 PGF/Tikz 节点)。

上述代码的输出


以下是相同输出的简化代码。

\documentclass{article}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{geometry}
\usepackage{nicematrix}
\usepackage{caption}

\begin{document}
\begin{table}[h!]
\centering
\caption{Composition of the study sample in the pre-Black Death period by site.}\label{Table5.2}

\begin{NiceTabular}{@{}>{\bfseries}l>{\bfseries}lc*{4}{w{c}{9mm}}@{}}
\CodeBefore
    \rowcolors[HTML]{3-13}{E7E6E6}{}[cols=2-7]
\Body
    \toprule
    \RowStyle[nb-rows=2,bold]{} 
    Region       & Site & \Block{1-2}{Early pre-\\ Black Death} && \Block{1-2}{Late pre-\\ Black Death} && Total\\
                 &                        & n   & \%    & n   & \%    & N    \\ \hline
    Extra-London & Wharram Percy          & 7   & 12.07 & 51  & 87.93 & 58   \\
                 & St Oswald's            & 44  & 29.33 & 106 & 70.67 & 150  \\
                 & St Anne's Charterhouse & 0   &       & 0   &       & 0    \\
                 & Barton Upon Humber     & 197 & 66.33 & 100 & 33.67 & 297  \\
                 & Stoke Quay             & 191 & 35.97 & 340 & 64.03 & 531  \\ \cline{2-7} 
                 & Subtotal               & 439 & 42.37 & 597 & 57.63 & 1036 \\
    \midrule
    London       & Guildhall Yard         & 10  & 27.03 & 27  & 72.97 & 37   \\
                 & East Smithfield        & 0   &       & 0   &       & 0    \\
                 & Merton Priory          & 0   & 0     & 37  & 100   & 37   \\
                 & St Mary Graces         & 0   &       & 0   &       & 0    \\
                 & Spital Square          & 0   &       & 40  &  100  & 40   \\ \cline{2-7} 
                 & Subtotal               & 10  & 8.77  & 104 & 91.23 & 114  \\ \hline
    Total        &                        & 449 & 39.04 & 701 & 60.96 & 1150 \\
    \bottomrule
\end{NiceTabular}

\end{table}
\end{document}

答案2

tabularray包装:

\documentclass{article}
\usepackage{geometry} % set page parameters suitably
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage[skip=0.33\baselineskip,
            font=small]{caption}
\usepackage{xcolor}
\newcommand\mycolor{\cellcolor[HTML]{E7E6E6}} % handy shortcut macro
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[ht]
    \centering
\caption{Composition of study sample in pre-Black Death period, by site.\strut}
\label{Table5.2}
\begin{tblr}{colspec = {ll *{2}{S[table-format=4.1] S[table-format=2.2]}
                                S[table-format=4.0]},
             row{1} = {guard, font=\bfseries},
             row{2} = {guard, mode=math},
             cell{odd[3-X]}{2-Z} = {gray9},
             }
    \toprule
\SetCell[c=1]{f}    Region  
    &   \SetCell[c=1]{f}    Site  
        &   \SetCell[c=2]{c}    {Early pre-\\ Black Death}
            &       &   \SetCell[c=2]{c}    {Late pre-\\ Black Death}
                            &       &   \SetCell[c=1]{f}    Total     
                                                        \\
    &       & n     & \%    & n     & \%    & N         \\

    \midrule
\SetCell[r=6]{h}   Extra-London 
    & Wharram Percy
            &   7   & 12.07 & 51    & 87.93 & 58        \\
    & St Oswald's 
            &  44   & 29.33 & 106   & 70.67 & 150       \\
    &  St Anne's Charterhouse 
            &  0&   &   0   &       &  0                \\
    & Barton Upon Humber
            & 197   & 66.33 & 100   & 33.67 & 297       \\
    & Stoke Quay
            &  191  & 35.97 &  340  & 64.03 & 531       \\
    \midrule
    & \textsc{Subtotal} 
            & 439   & 42.37 & 597   & 57.63 & 1036      \\
    \midrule
\SetCell[r=6]{h}   London 
    & Guildhall Yard
            &  10   & 27.03 &  27   & 72.97 &  37       \\
    & East Smithfield 
            & 0     &       & 0     &       & 0         \\
    &  Merton Priory
            & 0     & 0     & 37    &  100  & 37        \\
    & St Mary Graces 
            & 0     &       & 0     &       & 0         \\
    &  Spital Square 
            &  0    &       &  40   &  100  & 40       \\
    \midrule
    & \textsc{Subtotal}
            & 10    & 8.77  & 104   & 91.23 & 114       \\
    \midrule
\textsc{Total}  
    &       & 449   & 39.04 & 701   & 60.96 & 1150      \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

答案3

不要混用视觉分组习惯用法:如果您使用彩色(或灰色)行,请不要使用水平线,反之亦然。我建议您简单地省略所有\hline指令,并用 替换大多数\midrule指令\addlinespace

我也会摆脱所有大胆的。这根本就没有必要,而且确实会降低表格的可读性。如果您必须突出显示选定的单词(例如“Subtotal”和“Total”),请使用小写字母。而且,我会使用包的列类型\textsc将 5 个数据列中的数字对齐到其显式或隐式小数标记上。Ssiunitx

在此处输入图片描述

%!TeX program = pdflatex
\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\newcommand\mycolor{\cellcolor[HTML]{E7E6E6}} % handy shortcut macro
\usepackage{geometry} % set page parameters suitably
\usepackage{siunitx} % for 'S' column type
\newcolumntype{T}[1]{S[table-format=#1]}

\begin{document}
\begin{table}[ht]
\setlength\extrarowheight{1pt} % for a less-cramped look
\centering
\caption{Composition of study sample in pre-Black Death period, by site.\strut}
\label{Table5.2}
\begin{tabular}{@{} ll T{3.0} T{2.2} T{3.0} T{3.2} T{4.0}}
\toprule
Region & Site & 
\multicolumn{2}{c}{Early pre-} & \multicolumn{2}{c}{Late pre-} & Total \\
& & 
\multicolumn{2}{c}{Black Death} & \multicolumn{2}{c}{Black Death}& \\
\cmidrule(lr){3-4} \cmidrule(lr){5-6}
& & {$n$} & {\%} & {$n$} & {\%} & {$N$} \\ 
\addlinespace
Extra-London & 
  \mycolor Wharram Percy& \mycolor 7& \mycolor 12.07 & \mycolor 51& \mycolor 87.93 & \mycolor 58\\
& St Oswald's & 44& 29.33 & 106 & 70.67 & 150 \\
& \mycolor St Anne's Charterhouse & \mycolor 0& \mycolor & \mycolor 0& \mycolor & \mycolor 0\\
& Barton Upon Humber& 197 & 66.33 & 100 & 33.67 & 297 \\
& \mycolor Stoke Quay& \mycolor 191 & \mycolor 35.97 & \mycolor 340 & \mycolor 64.03 & \mycolor 531 \\
& \textsc{Subtotal} & 439 & 42.37 & 597 & 57.63 & 1036 \\
\addlinespace
London & 
  \mycolor Guildhall Yard& \mycolor 10& \mycolor 27.03 & \mycolor 27& \mycolor 72.97 & \mycolor 37\\
& East Smithfield & 0&& 0&& 0\\
& \mycolor Merton Priory& \mycolor 0& \mycolor 0& \mycolor 37& \mycolor 100& \mycolor 37\\
& St Mary Graces & 0&& 0&& 0\\
& \mycolor Spital Square & \mycolor 0 & \mycolor & \mycolor 40& \mycolor 100 & \mycolor 40\\  
& \textsc{Subtotal}& 10& 8.77& 104 & 91.23 & 114 \\ 
\midrule
\textsc{Total} & & 449 & 39.04 & 701 & 60.96 & 1150\\
\bottomrule
\end{tabular}
\end{table}
\end{document}

附录回应原帖者的后续评论。你写道:“很遗憾,决赛桌没有我想要的格式。”遗憾的是,我也不知道你想要哪种格式。;-) 但坦率地说,我从来都不擅长读懂别人的想法……

就我个人而言,我不能说交替的灰色和白色行对增强和澄清表格内容有多大作用。因此,我强烈建议您考虑完全放弃交替的白色/灰色行 - 并使用更多的水平规则。例如,像这样:

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs,geometry,siunitx} 
\newcolumntype{T}[1]{S[table-format=#1]}

\begin{document}
\begin{table}[ht]
\centering
\caption{Composition of study sample in two pre-Black Death periods, by site.\strut}
\label{Table5.2}
\begin{tabular}{@{} ll T{3.0} T{2.2} T{3.0} T{3.2} T{4.0} @{}}
\toprule
Region & Site &
\multicolumn{2}{c}{Early pre-} & \multicolumn{2}{c}{Late pre-} & {Total} \\
& &
\multicolumn{2}{c}{Black Death} & \multicolumn{2}{c}{Black Death}& \\
\cmidrule(lr){3-4} \cmidrule(lr){5-6}
& & {$n$} & {\%} & {$n$} & {\%} & {$N$} \\
\midrule
Extra-London &
  Wharram Percy     &   7 & 12.07 &  51 & 87.93 &   58 \\
& St Oswald's       &  44 & 29.33 & 106 & 70.67 &  150 \\
& St Anne's Charterhouse & 0 &    &   0 &       &    0 \\
& Barton Upon Humber& 197 & 66.33 & 100 & 33.67 &  297 \\
& Stoke Quay        & 191 & 35.97 & 340 & 64.03 &  531 \\[0.5ex]
& \textsc{Subtotal} & 439 & 42.37 & 597 & 57.63 & 1036 \\
\midrule
London &
  Guildhall Yard    & 10 & 27.03 &  27 &  72.97 &   37 \\
& East Smithfield   &  0 &       &   0 &        &    0 \\
& Merton Priory     &  0 &  0    &  37 & 100    &   37 \\
& St Mary Graces    &  0 &       &   0 &        &    0 \\
& Spital Square     &  0 &       &  40 & 100    &   40 \\[0.5ex]
& \textsc{Subtotal} & 10 &  8.77 & 104 &  91.23 &  114 \\
\midrule
Total & &            449 & 39.04 & 701 &  60.96 & 1150 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

相关内容