Can someone help with code this table in LaTeX?
-
4Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. There are many examples of multirow and multicolumn table on this site, see here: tex.stackexchange.com/a/167367/101651 for example. Please try to do something by yourself and then add a minimal working example (MWE) to your post. As is, your question is a just-do-it-for-me one.– CarLaTeXNov 17, 2017 at 7:45
2 Answers
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}
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
.
A simple way is to use the package multirow (http://ftp.fau.de/ctan/macros/latex/contrib/multirow/multirow.pdf) and if needed tabularx. It provides the possibilities to combine cells in a row or column.
Edit: A code example (sry for forgetting that). It's not the hole table but it should give a feeling what to do with multicolumn and multirow.
\documentclass{scrartcl}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ll|ll|}
\cline{3-4}
& & \multicolumn{2}{c|}{$Y$}\\
\multirow{3}{*}{X} & & &a\\\cline{4-4}
& & &b \\ \cline{4-4}
& & &c \\ \cline{4-4}
\end{tabular}
\end{document}
-
4That sounds like a good idea. And even though the the question does not have a lot of code in it, your answer could definitely benefit from expansion and a code example. At the moment it feels more like a comment.– moeweNov 17, 2017 at 9:26