0

I have made a table with a table generator. I want the two first rows to be white, the following row to be grey and white intermittently.

This is the code I generated:

Preamble: (long since this is a long document)

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

The problem is that the colour starts one row before I wanted it to start. The second row with n and % symbols should be white and the penultimate row show also be coloured. I cannot understand why this happens. I can't see any code indicating for the second row to be coloured.

I also get an error in:

\multicolumn{12}{l}{\footnotesize{\textsuperscript{1.}{T=Tuberculosis}}} which says misplaced \omit.

4
  • Try switching to nicematrix or tabularray package, they're generally nicer.
    – user202729
    May 15 at 11:23
  • 3
    Also don't just include snippets, include a minimal working example. → Please write a minimal working example. See link for more details what it means.
    – user202729
    May 15 at 11:23
  • For the error, you have written \\hline instead of \\ \hline at the end of your tabular. May 15 at 12:35
  • 2
    Please note that scaling a table using \resizebox or \begin{adjustbox} is discouraged and leads to bad typography.
    – Skillmon
    May 15 at 13:02

1 Answer 1

1

I'm not sure to understand exactly what you want.

Here is a solution with {NiceTabular*} of 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}

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

Output of the above code

3
  • Thank you! I am sorry about the confusing question. I realised late that I had copied the wrong table (I accidentally had labelled them both the same). Nevertheless, this helps me having less messy tables. Can I ask you 2 questions? 1. Is there a way to move extra-London and London to the top (next to Pre-Black Death) and move Subtotal to be aligned with Total. 2. I have several tables I would like to convert to this format. I normally use a table generator to ensure I won't make any mistake with the data. Did you do this by hand or is there a more efficient way to copy all the data?
    – Flor
    May 15 at 15:21
  • I've modified my answer to put "London" and "extra-London" as you wish. I don't use table generator. Usually, it's easy to compose directly the tabular in LaTeX. May 15 at 19:18
  • Thank you. I will try to make tables using nicematrix.
    – Flor
    May 19 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.