# How can I create this type of mutlicolumn and multirow table?

I want to create this table in LaTex.

• Welcome to TSE. What did you try? Aug 4 at 14:45
• Okay, I get it. The rows mean nothing. This is just 6 completely separate tabulars side by side. Aug 4 at 21:54
• Off topic: If the rows mean nothing, a table is probably not the most suitable way to typeset this data ... Aug 5 at 17:42

Probably you can achieve what you need by nesting tables and playing with \arraystretch, \hphantom and \rlap.

\RequirePackage{array}
\documentclass[12pt]{article}
\usepackage[table]{xcolor}
\newlength\scratchlength

\definecolor{LightGrayCellColor}{RGB}{251 251 251}
\definecolor{DarkGrayCellColor}{RGB}{245 245 245}
\definecolor{RuleColor}{RGB}{232 232 232}
\definecolor{TextColor}{RGB}{64 64 64}

\begin{document}

\begingroup
\arrayrulecolor{RuleColor}%
\color{TextColor}%
\renewcommand{\arraystretch}{2}%
%\arrayrulewidth=2\arrayrulewidth % <- increase thickness of lines in
% case by your pdf-viewer they are not displayed correctly with
% small zoom-factors.
\noindent
\begin{tabular}{@{}llllll@{}}%
\hline
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Vegetables}}\\%
\hline
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Annual}}\hphantom{Beetroot}}&%
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Perennial}}\hphantom{Capsicum}}%
\end{tabular}%
}%
&%
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Herbs}}\\%
\hline
\cellcolor{LightGrayCellColor}\rlap{\textsf{Annual}}\hphantom{Fenugreek}&%
\cellcolor{LightGrayCellColor}\rlap{\textsf{Perennial}}\hphantom{Holy Basil}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Fruits}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Flowers}%
\end{tabular}%
}%
\\%
\hline
\multicolumn{1}{|l}{%
\cellcolor{DarkGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
Beans\\%
\cellcolor{LightGrayCellColor}Beetroot\settowidth\scratchlength{Melon}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bitter\hfill\null\linebreak Melon\strut}\\%
\cellcolor{LightGrayCellColor}Carrot\settowidth\scratchlength{Gourd}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bottle\hfill\null\linebreak Gourd\strut}\\%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Capsicum\\%
Chilli\\%
\cellcolor{DarkGrayCellColor}Pepper%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Coriander\\%
Fenugreek\\%
\cellcolor{DarkGrayCellColor}Garlic\\%
Basil\\%
\cellcolor{DarkGrayCellColor}Carom\\%
Dill%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Ginger\\%
Saffron\\%
\cellcolor{DarkGrayCellColor}Holy Basil\\%
Thyme\\%
\cellcolor{DarkGrayCellColor}Turmeric\\%
Rosemary%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Strawberry\\%
Blueberry\\%
\cellcolor{DarkGrayCellColor}Fig\\%
Tomato\\%
\cellcolor{DarkGrayCellColor}Pineapple\\%
Cantaloupe%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Petunia\\%
Geranium\\%
\cellcolor{DarkGrayCellColor}Begonia\\%
Lobelia\\%
\cellcolor{DarkGrayCellColor}Pansy\\%
Impatiens%
\end{tabular}%
}\\%
\hline
\end{tabular}%
\endgroup

\end{document}


Using pgfmath you can calculate \arraystretch and \extrarowheight so that the vertical gap between top-rule of cell and uppercase-letters is approximately the same as the vertical gap between lowercase-letters and bottom-rule of cell, namely the difference between the depth of strutbox and depth of letters like yjpqfgj scaled so that total height and depth of single-line tablecells is 2\baselineskip while the strutbox and \baselineskip can be adjusted, via \fontsize.
(Total height and depth of single-line tablecells must be 2\baselineskip because in the leftmost-column you have 5 rows whereof two are vertically larger by \baselineskip and in the other columns you have six rows which makes 5rows+2\baselineskip=6rows→2\baselineskip=1row...)

\RequirePackage{array}
\documentclass[12pt]{article}
\usepackage[table]{xcolor}
\usepackage{pgfmath}
\newlength\scratchlength

\definecolor{LightGrayCellColor}{RGB}{251 251 251}
\definecolor{DarkGrayCellColor}{RGB}{245 245 245}
\definecolor{RuleColor}{RGB}{232 232 232}
\definecolor{TextColor}{RGB}{64 64 64}

\begin{document}
\makeatletter
\begingroup
\arrayrulecolor{RuleColor}%
\color{TextColor}%
\fontsize{12pt}{12pt}\selectfont
\settoheight\scratchlength{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}%
\extrarowheight=\scratchlength
\settodepth\scratchlength{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}%
\pgfmathparse{((2\baselineskip-\extrarowheight)/(2\dp\strutbox))}%
\let\arraystretch\pgfmathresult
\pgfmathparse{(1/(\arraystretch))}%
\extrarowheight=\pgfmathresult\extrarowheight
%\arrayrulewidth=2\arrayrulewidth % <- increase thickness of lines in
% case by your pdf-viewer they are not displayed correctly with
% small zoom-factors.
\noindent
\begin{tabular}{@{}llllll@{}}%
\hline
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Vegetables}}\\%
\hline
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Annual}}\hphantom{Beetroot}}&%
\cellcolor{LightGrayCellColor}\hbox{\rlap{\textsf{Perennial}}\hphantom{Capsicum}}%
\end{tabular}%
}%
&%
\multicolumn{2}{|@{}l@{}}{%
\begin{tabular}[c]{l|l}%
\multicolumn{2}{l}{\cellcolor{LightGrayCellColor}\textsf{Herbs}}\\%
\hline
\cellcolor{LightGrayCellColor}\rlap{\textsf{Annual}}\hphantom{Fenugreek}&%
\cellcolor{LightGrayCellColor}\rlap{\textsf{Perennial}}\hphantom{Holy Basil}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Fruits}%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[c]{@{}l@{}}%
\textsf{Flowers}%
\end{tabular}%
}%
\\%
\hline
\multicolumn{1}{|l}{%
\cellcolor{DarkGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
Beans\\%
\cellcolor{LightGrayCellColor}Beetroot\settowidth\scratchlength{Melon}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bitter\hfill\null\linebreak Melon}\\%
\cellcolor{LightGrayCellColor}Carrot\settowidth\scratchlength{Gourd}\global\scratchlength\scratchlength\\%
\multicolumn{1}{@{}p{\scratchlength}}{Bottle\hfill\null\linebreak Gourd}\\%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Capsicum\\%
Chilli\\%
\cellcolor{DarkGrayCellColor}Pepper%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Coriander\\%
Fenugreek\\%
\cellcolor{DarkGrayCellColor}Garlic\\%
Basil\\%
\cellcolor{DarkGrayCellColor}Carom\\%
Dill%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Ginger\\%
Saffron\\%
\cellcolor{DarkGrayCellColor}Holy Basil\\%
Thyme\\%
\cellcolor{DarkGrayCellColor}Turmeric\\%
Rosemary%
\end{tabular}%
}%
&%
\multicolumn{1}{|l}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Strawberry\\%
Blueberry\\%
\cellcolor{DarkGrayCellColor}Fig\\%
Tomato\\%
\cellcolor{DarkGrayCellColor}Pineapple\\%
Cantaloupe%
\end{tabular}%
}%
&%
\multicolumn{1}{|l|}{%
\cellcolor{LightGrayCellColor}%
\begin{tabular}[t]{@{}l@{}}%
\cellcolor{DarkGrayCellColor}Petunia\\%
Geranium\\%
\cellcolor{DarkGrayCellColor}Begonia\\%
Lobelia\\%
\cellcolor{DarkGrayCellColor}Pansy\\%
Impatiens%
\end{tabular}%
}\\%
\hline
\end{tabular}%
\endgroup

\end{document}


• Excellent for your efforts. It is very similar to the original :-) Aug 5 at 12:24
• @Sebastiano Thanks and yes: I copied as best I could in a rush an original that looks terrible. :-)) Aug 5 at 12:30

It is easier to create this type of tables with the new LaTeX3 package tabularray:

\documentclass{article}

\usepackage{tabularray,xcolor}

\begin{document}

\begin{tblr}{
vspan=even, hlines={white,1pt}, vlines = {white,1pt},
row{1-2} = {font=\sffamily,azure9},
row{3} = {gray9}, row{4-12} = {gray9!50},
cell{1}{1,3} = {c=2}{}, cell{1}{5,6} = {r=2}{},
cell{5,10}{1} = {r=3}{gray9}, cell{8}{1} = {r=2}{},
cell{5,7,9,11}{2-6} = {r=2}{},
cell{5}{2-6} = {gray9}, cell{9}{3-6} = {gray9},
}
Vegetables        &           & Herbs     &            & Fruits     & Flowers   \\
Annual            & Perennial & Annual    & Perennial  &            &           \\
Beans             & Capsicum  & Coriander & Ginger     & Strawberry & Petunia   \\
Beetroot          & Chilli    & Fenugreek & Saffron    & Blueberry  & Geranium  \\
{Bitter \\ Melon} & Pepper    & Garlic    & Holy Basil & Fig        & Begonia   \\
&           &           &            &            &           \\
&           & Basil     & Thyme      & Tomato     & Lobelia   \\
Carrot            &           &           &            &            &           \\
&           & Carom     & Turmeric   & Pineapple  & Pansy     \\
{Bottle \\ Gourd} &           &           &            &            &           \\
&           & Dill      & Rosemary   & Cantaloupe & Impatiens \\
&           &           &            &            &           \\
\end{tblr}

\end{document}


Here is a solution with {NiceTabular} of nicematrix.

The rules won't seem to vanish, whatever PDF viewer is used.

\documentclass{article}
\usepackage{nicematrix,tikz,makecell}

\begin{document}

\definecolor{gray1}{gray}{0.8}
\definecolor{gray2}{gray}{0.9}

\begin{NiceTabular}{llllll}[hvlines,cell-space-limits=7pt,colortbl-like]
\CodeBefore
\rowcolors{3}{gray1}{gray2}
\tikz \fill [gray1] (5-|1) rectangle (6.5-|2) (7.5-|1) rectangle (9-|2) ;
\tikz \fill [gray2] (6.5-|1) rectangle (7.5-|2) ;
\Body
\Block[l]{1-2}{Vegetable} & & \Block[l]{1-2}{Herbs} & & \Block{2-1}{Fruits} & \Block{2-1}{Flowers} \\
Annual    & Perennial    & Annual             & Perennial  & \\
Beans     & Capsicum     & Coriander          & Ginger     & Strawberry          & Petunia \\
Beetroot  & Chilli       & Fenugreek          & Saffron    & Blueberry           & Geranium \\
\smash{\makecell[t]{Bitter\\ Melon}}
\Block{4-1}{}
& Pepper       & Garlic             & Holy Basil & Fig                 & Begonia \\
\Block{2-1}{Carrot}
& \Block[fill=gray2]{3-1}{}
& Basil              & Thyme      & Tomato              & Lobelia \\
&              & Carom              & Turmeric   & Pineapple           & Pansy \\
\smash{\makecell[b]{Bottle\\ Gourd}}
&              & Dill               & Rosemary   & Cantaloupe          & Impatiens
\CodeAfter
\tikz \draw (6.5-|1) -- (6.5-|2) (7.5-|1) -- (7.5-|2) ;
\end{NiceTabular}

\end{document}


You need several compilations (because of the PGF/Tikz nodes).

While both of these other answers work, they are (in my opinion) unnecessarily difficult. Why not just use the easy online table generator: https://www.tablesgenerator.com/ ?

It lets you create the table (pretty much WYSIWYG) and then you copy/paste the latex source code. Way easier (and easier to debug, imo) than manually typing all that latex code.

• Can you provide an example created by the table-generator where the entries "Bitter Melon" and "Bottle Gourd" in the leftmost column are vertically aligned as in the image provided by the questioner? Aug 5 at 12:23
• What do you mean vertically aligned? They appear (to me at least) to be vertically centered and left-aligned? Aug 5 at 12:30
• In the row containing "Bitter Melon" baselines of single-line cells are vertically aligned with the baseline of the word "Bitter"/with the baseline of the first line of the two-lines-cell. In the row containing "Bottle Gourd" baselines of single-line cells are vertically aligned with the baseline of the word "Gourd"/with the baseline of the second line of the two-lines-cell. Aug 5 at 12:34
• Ok, you mean how some columns contain 5 elements and some 6 but the sizes are such that the both fill the space? The question wasn't clear (to me) that that was required--probably a poor design choice (imo). That is harder to do with the table generator (in fact I don't see a way to do it without manually modifying the latex source code). Aug 5 at 12:45
• You are absolutely right - the question wasn't clear to me either - just "How can I create this type of table?" It is unclear what is meant by "type", or to what extent it is important to reproduce the appearance of the original as exactly as possible. So I based my approach as closely as I could on the look of the original -eh- design, although - as indicated in my other comment - I think there is room for improvement of that. :-> Aug 5 at 12:57