2

I've created this table using a table converter website online and then tweaking it until I got what I wanted. I am using overleaf to compile everything.

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

The table looks messy, but it compiles. The issue that I have is that after compiling, the table looks like this:

Table showing how the previous code compiles

It has all the information I want, but I have a few issues:

  1. All lines came out right except the second one on top of Extra-London. It should go across the whole graph, but it only covers one cell.
  2. There is a small white gap under the midline )the one that starts with London). I have seen this a lot and don't know how to remove it.
  3. The colored cells extend further than the lines and margin. I want the lines and cells to have the same limit at the end of the page's margins.
  4. I can't be sure but it looks like all the cells don't have the same height, The second subtotal looks shorter than the first one (I may be wrong).

How can I fix these issues? Any advice will be welcomed.

Thank you, Sincerely,

4
  • Can you please make your code snippet be compilable? Then we do not have to guess what you are doing ...
    – Mensch
    Commented Aug 15, 2022 at 18:40
  • 1
    \textbf{} does nothing, you could simply delete them all, mak your source look a lot more reasonable please see the colortbl documentation, \cline is explicitly not supported. There are things you coud do (use hhline, or use different table markup such as tabularray But are you sure you want rules and coloured panels, I'd just not use the rules Commented Aug 15, 2022 at 18:40
  • 1
    Welcome to TeX.SX! Don't mix \hline with \toprule, \midrule and \bottomrule because the latter three (which are defined by the booktabs package) have a different spacing. In the case of colored cells, this spacing may look odd. Commented Aug 15, 2022 at 18:41
  • urelated but don't use [h!] unless you really need an exceptional over-ride (!) Commented Aug 15, 2022 at 18:42

3 Answers 3

1

Mico's answer is probably the good answer (+1) but, if you actually want to have the design you have in mind in your question, you can use {NiceTabular} of nicematrix.

In the following example, I have used your code by replacing {tabular} by {NiceTabular} (with the key 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}

However, you need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code


Here is a simplified code for the same output.

\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}
1
  • Thank you! I also liked the previous answer. However, it changed the format of the table too much. I've tried to use Nicematric before but had some compilation issues. I will try again.
    – Flor
    Commented Aug 16, 2022 at 13:57
1

With tabularray package:

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

enter image description here

1

Don't mix-and-mismatch visual grouping idioms: If you use colored (or gray) rows, don't use horizontal lines, and vice versa. I recommend you simply omit all \hline directives and replace most \midrule directives with \addlinespace.

I would also get rid of all bold-facing. It's simply not needed, and it does worsen the table's legibility. If you must highlight select words (such as "Subtotal" and "Total", use \textsc for small-caps. And, I'd align the numbers in the 5 data columns on their expicit or implicit decimal markers, using the S column type of the siunitx package.

enter image description here

%!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}

Addendum to address the OP's follow-up comment. You write that "Regretfully, the final table does not have the format I want." Sadly, I don't know either which format you want. ;-) But then, frankly, I've never been any good at reading other people's minds...

Speaking for myself, I can't say that the alternating gray and white rows do much to enhance and clarify the table's contents. I would therefore seriously recommend you consider ditching the alternating white/gray rows altogether -- and use more horizonal rules instead. I.e., something like this:

enter image description here

\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}
2
  • Thank you! This is very useful. The \mycolor command could make things much easier. The top/mid and bottom lines were a last-minute addition when I started to have issues with \hline. Regretfully, the final table does not have the format I want. Even if I removed the bold fonts, I still need the lines to be as I placed them. The addlinespace as you did does not work. Is there any other way to add additional lines that look exactly like the top and bottom lines?
    – Flor
    Commented Aug 16, 2022 at 13:53
  • @Flor - Please see the addendum I just posted.
    – Mico
    Commented Aug 16, 2022 at 14:44

You must log in to answer this question.

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