[Tex/LaTex] LaTeX code for table with multiple rows and columns

multicolumnmultirowtables

Can someone help with code this table in LaTeX?

enter image description here

Best Answer

A couple of solutions, with or without vertical rules:

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{multirow}
\newcolumntype{L}{>{$}l<{$}}
\newcolumntype{C}{>{$}c<{$}}
\renewcommand*{\arraystretch}{1.4}
\usepackage{caption}

\begin{document}
Table~\ref{yours} is as your image.
\begin{table}
    \centering
    \caption{Categorical classification of variables\label{yours}}
    \begin{tabular}{|LL|LL|C|C|}
        \cline{5-6}
        \multicolumn{4}{c|}{}& \multicolumn{2}{C|}{Y_{i}}\\
        \hline
        \multicolumn{4}{|c|}{}&\text{Yes $(1)$}& \text{No $(0)$} \\
        \hline
        \multicolumn{2}{|c|}{}&\multicolumn{1}{c}{}& 1 & a & b \\
        \cline{4-6}
        \multirow{2}{*}{$X_{i}$} & 0 & X_{i_{1}} & 2 & c & d \\
        \cline{2-6}
        & 1 & X_{i_{2}} & 1 & e & f \\
        \cline{4-6} 
        &&& 2 & g & h \\ 
        \hline
    \end{tabular}
\end{table}
Table~\ref{mine} is without vertical rules.
\begin{table}
    \centering
    \caption{Categorical classification of variables\label{mine}}
    \begin{tabular}{LLLLCC}
        \multicolumn{4}{c}{}& \multicolumn{2}{C}{Y_{i}}\\
        \cline{5-6}
        \multicolumn{4}{c}{}&\text{Yes $(1)$}& \text{No $(0)$} \\
        \hline
        \multicolumn{2}{c}{}&\multicolumn{1}{c}{}& 1 & a & b \\
        \cline{4-6}
        \multirow{2}{*}{$X_{i}$} & 0 & X_{i_{1}} & 2 & c & d \\
        \cline{2-6}
        & 1 & X_{i_{2}} & 1 & e & f \\
        \cline{4-6} 
        &&& 2 & g & h \\ 
        \hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

If the cell content is plain text instead of in math mode, just change L and C (the new column types I created) into l and c.

Related Question