# To make a table in Latex in desired format

I want to draw a table in the following pattern.

I am new to making tables using Latex. I just know the following way.

\begin{table}[!h]
\begin{tabular}{c| c c c c}
\hline
% Data to enter here
\hline 2
\end{tabular}
\end{table}


Kindly help me. Thanks a lot. I am using WinEdt editor.

• unrelated but it's usually not a good idea to use [!h] latex will warn and change it to [!ht] but removing p makes it a lot more likely that the figure goes to the end, and ! should only be needed vary rarely, so [htp] is usually a better choice. – David Carlisle Jun 5 '18 at 15:32

i would consider the following table layout:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs, makecell}
\newcommand\mcc[1]{\multicolumn{2}{>{$}c<{$}}{#1}}

\begin{document}
\begin{table}
\centering
\begin{tabular}{>{$}r<{$}*{6}{>{$}c<{$}}}
\toprule
& \multicolumn{6}{c}{$\max i$}                                              \\
\cmidrule{2-7}
& \mcc{i\equiv0\pmod4} & \mcc{i \equiv1\pmod 4} & \mcc{i\equiv0\pmod 4}     \\
\cmidrule(r){2-3}\cmidrule(lr){4-5}\cmidrule(l){6-7}
\multicolumn{1}{c}{$n$}
& m\text{ even}
& m\text{ odd}      & m\text{ even}
& m\text{ odd}      & m\text{ even}
& m\text{ odd}          \\
\midrule
4n      & 2m    & 2m-2          & -     & -             & -     & -                 \\
4n +1   & -     & -             & -     & -             & -     & -                 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}


edit: pmod is stolen from Bernard answer :-)

adding one column pairs to above table widens it. this can cause the table to be wider than the text and will consequently protrude right text margin (your page layout is not known, since you not provide complete small document -- mwe -- with your table code snippet; now you can see how important this is).

assuming, that text width in your document as is default provided by article. in this case is necessary to change spaces between columns. in this is sensible to left this to latex with the replacing

\centering
\begin{tabular}{>{$}r<{$}*{6}{>{$}c<{$}}}


to

\setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
>{$}r<{$}
*{8}{>{$}c<{$}}}


where @{\extracolsep{\fill}} accommodate \tabcolsep (spaces between columns) to value, which enable to fit table in text width (if the width of the text is large enough). complete mwe is:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs, makecell}
\newcommand\mcc[1]{\multicolumn{2}{>{$}c<{$}}{#1}}

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

\begin{document}
\begin{table}
\setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
>{$}r<{$}
*{8}{>{$}c<{$}}}
\toprule
& \multicolumn{8}{c}{$\max i$}                                              \\
\cmidrule{2-9}
& \mcc{i\equiv0\pmod4} & \mcc{i \equiv1\pmod 4} & \mcc{i\equiv0\pmod 4} & \mcc{i\equiv2\pmod 4}     \\
\cmidrule(r){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(l){8-9}
\multicolumn{1}{c}{$n$}
& m\text{ even}
& m\text{ odd}      & m\text{ even}
& m\text{ odd}      & m\text{ even}
& m\text{ odd}
& m\text{ even}
& m\text{ odd}          \\
\midrule
4n      & 2m    & 2m-2          & -     & -             & -     & -             & -     & -                 \\
4n +1   & -     & -             & -     & -             & -     & -             & -     & -                 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}


(red lines indicate page layout)

note: in table design you first select type of table. beside of basic tabular are other possibilities: tabular* (which has prescribed width, see mwe anove), tabularx, etc. with its options, determine the number and type of columns. for example:

\begin{tabular}{cccc}


is basic table with four columns. it also can be write as

\begin{tabular}{*{4}{c}}


\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
>{$}r<{$}
*{8}{>{$}c<{$}}}


has nine columns and table width is \linewidth (which is value of temporary width of environment where the table is placed. in your case it is equal to \textwidth.

• this are two columns :-). i guea that this columns are added on the right side ... see addendum to answer (will appear asap). – Zarko Jun 6 '18 at 6:48

With diagbox and amsmath:

\documentclass[11pt,pdftex, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{diagbox}

\begin{document}

$\begin{array}{c|*{3}{cc|}} &\multicolumn{2}{c|}{i\equiv 0\pmod 4} &\multicolumn{2}{c|}{i\equiv 1\pmod 4} &\multicolumn{2}{c|}{i\equiv 2\pmod 4} \\$\smash[t]{ \diagbox[ height=1.cm, width = 1.9cm, innerleftsep =0.6cm, innerrightsep = 0cm]{\hfill$n$}{$\max i$ }}$% & m\:\text{ even,} & m\:\text{ odd} & m\:\text{ even,} & m\:\text{ odd} & m\:\text{ even,} & m\:\text{ odd}\\ \hline 4n & 2m & 2m-2 & - & - & - & - \\ \hline 4n + 1 & - & - & - & - & - & - \\ \hline 4n + 3 & - & - & - & - & - & - \\ \hline \end{array}$


\end{document}

Something like this?

\documentclass{article}
\usepackage{diagbox}
\usepackage{multirow}

\begin{document}
\begin{table}
\centering
\begin{tabular}{c|cc|cc|cc}
\multirow{2}{*}{\backslashbox{bottom text}{top text}} & \multicolumn{2}{c|}{\begin{tabular}[c]{@{}l@{}}h1\\ \end{tabular}} & \multicolumn{2}{c|}{h2} & \multicolumn{2}{c}{h3}  \\
& sub11 & sub12                                                      & sub21 & sub22           & sub31 & sub32           \\
\hline
r1                                                      & c11   & c12                                                        & c13   & c14             & c15   & c16             \\
r2                                                      & c21   & c22                                                        & c23   & c24             & c25   & c26             \\
r3                                                      & c31   & c32                                                        & c33   & c34             & c35   & c36
\end{tabular}
\end{table}
\end{document}


You could just fill it by yourself.