0

I am trying to create a hierarchical table like below:

enter image description here

The code I am working with is given below:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{array, booktabs, ragged2e}
\title{table}
\author{}
\date{}

\begin{document}

\maketitle

\section{Introduction}



\begin{tabular}{p{1ex}>{\RaggedRight}p{4cm}*{3}{p{1.8cm}p{1cm}}}\toprule
\multicolumn{2}{c}{Variables} & \multicolumn{2}{c}{Level of precipitation} & 
\multicolumn{4}{c}{} \\\cmidrule{3-8}
&           & \multicolumn{2}{c}{Monthly} & \multicolumn{2}{c}{Yearly}  \\
\cmidrule{3-4} \cmidrule(lr){5-6} 
&            & City-A & City-B & City-A & City-B &  &  \\\midrule
%\multicolumn{2}{c}{Female} & \\
& 1 & - & - & -  & - & - & -  \\ \midrule
& 2 & - & - & -  & - & - & -  \\ \midrule
\end{tabular}
\end{document}

Can someone please help to fix this code to get the desired output?

6
  • I don't understand why you do things the way you do them. If the image exactly represents what you want, then you have a lot of unnecessary things in the code.
    – Niranjan
    Dec 18, 2023 at 3:43
  • The image is generated in MSWord, where I need it in LaTeX.
    – felix
    Dec 18, 2023 at 3:45
  • Yes, agreed, but the code you have provided (which should show your attempt of reproducing a desired output) doesn't at all seem to be going the way you want.
    – Niranjan
    Dec 18, 2023 at 3:47
  • What about this?
    – Werner
    Dec 18, 2023 at 3:48
  • 1
    I advise you to go through this: learnlatex.org/en/lesson-08. In order to produce the desired output, you need to be clear with a lot of basics. This site wouldn't encourage questions which lack actual efforts. Your question looks like a "do-it-for-me" question. I vote to close it.
    – Niranjan
    Dec 18, 2023 at 3:51

1 Answer 1

2

Since you're using booktabs, the following aims to use that format to replicate the table.

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{tabular}{ *{5}{c} }
  \toprule
    & \multicolumn{4}{c}{\bfseries Level of precipitation} \\
  \cmidrule(lr){2-5}
    & \multicolumn{2}{c}{\itshape Monthly} & \multicolumn{2}{c}{\itshape Yearly} \\
  \cmidrule(lr){2-3} \cmidrule(lr){4-5}
  \bfseries Variables & City-A & City-B & City-A & City-B \\
  \midrule
  X & - & - & - & - \\
  Y & - & - & - & - \\
  \bottomrule
\end{tabular}

\end{document}
3
  • Can vertical lines be added to separate different cells?
    – felix
    Dec 18, 2023 at 4:59
  • 1
    the general advice is: you should never need vertical lines to separate cells. Dec 18, 2023 at 5:28
  • @felix: tabulars are necessarily columnar in structure, with content lined up beneath one another. This suggests a natural delineation using whitespace. If you wish to add vertical lines using a booktabs-like interface, then consider the offerings of tabularray. See this as an example.
    – Werner
    Dec 18, 2023 at 5:36

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