I am trying to create the table below, someone please help.
2 Answers
This post will probably be closed as it is a duplicate of a lot of others answers, doesn't show any research-effort and is basicly a question regarded as "Do my work for me".
You should have a look at same basic LaTeX-examples, books etc. Have a look at Getting Started with TeX, LaTeX, and Friends (Tug.org) for multiple great starter-tips.
Anyway, here is part of what you were looking for:
Output
Code
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}
\begin{tabular}{ccccc}
\toprule
& \multicolumn{2}{c}{Y} & \multicolumn{2}{c}{Y} \\
\addlinespace
& X & X & X & X \\
\midrule
A & 1 & 2 & 5 & 6 \\
B & 2 & 4 & 7 & 8 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
I guess you're trying to learn how to use the \multicolumn
directive correctly. The following code presents three possible solutions.
The solutions try to adhere to the format suggested in screenshot you've posted. Do note that tables which feature lots of vertical and horizontal lines are quite unappealing visually. Moreover, they tend to be needlessly hard to fathom by your readers. Consider @RunarTrollet's approach instead.
\documentclass{article}
\usepackage{array} % for \newcolumntype and \extrarowheight macros
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}
\setlength\extrarowheight{2.5pt} % for a (slightly!) more "open" look...
%% Version 1: Use five "c" columns
\begin{tabular}{|*{5}{c|}}
\cline{2-5}
\multicolumn{1}{c|}{} & \multicolumn{2}{c|}{Y} & \multicolumn{2}{c|}{Y} \\
\cline{2-5}
\multicolumn{1}{c|}{} & X & X & X & X \\
\hline
A & 1 & 2 & 5 & 6 \\
\hline
B & 2 & 4 & 7 & 8\\
\hline
\end{tabular}
%% Version 2: Use five "C" columns, each column is 1cm wide
\bigskip\bigskip
\begin{tabular}{|*{5}{C{1cm}|}}
\cline{2-5}
\multicolumn{1}{c|}{} & \multicolumn{2}{c|}{Y} & \multicolumn{2}{c|}{Y} \\
\cline{2-5}
\multicolumn{1}{c|}{} & X & X & X & X \\
\hline
A & 1 & 2 & 5 & 6 \\
\hline
B & 2 & 4 & 7 & 8\\
\hline
\end{tabular}
%% Version 3: Use five "C" columns, each column is 2cm wide
\bigskip\bigskip
\begin{tabular}{|*{5}{C{2cm}|}}
\cline{2-5}
\multicolumn{1}{c|}{} & \multicolumn{2}{c|}{Y} & \multicolumn{2}{c|}{Y} \\
\cline{2-5}
\multicolumn{1}{c|}{} & X & X & X & X \\
\hline
A & 1 & 2 & 5 & 6 \\
\hline
B & 2 & 4 & 7 & 8\\
\hline
\end{tabular}
\end{table}
\end{document}
-
I tried various examples but got some weird tables but this works. Thanks. Aug 22, 2016 at 18:46
-
2@GregorywantstolearnR, you should probably accept this answer if it solved your problem.– RunarAug 25, 2016 at 18:49