How can I have a table like the following picture in latex?

3 Answers
A possible solution. I suggest not using the vertical lines, but rather the booktabs package. For horizontal alignment of 20 you can exploit the multirow package, while for vertically alignment you can compute the width of the largest entry of the first columns via the calc package.

\documentclass{article}
\usepackage{booktabs,calc,multirow}
\newlength\wdx% for vertical alignment
\setlength{\wdx}{\widthof{66}}
\begin{document}
\begin{table}
\centering
\addtolength{\tabcolsep}{10pt}% increase distance between cells
\begin{tabular}{cccccc}
\toprule
\textsf{x} & \multicolumn{5}{c}{\textsf{y}}\\
\cmidrule(l{\wdx}r{\wdx}){1-1}\cmidrule(l){2-6}
10 & 22 & 33 \\
\multirow{2}[2]{\wdx}{20} & 45 & 34 & 88 & 77 & 80 \\
& 34 & 67\\
66 & 12 & 10 & 45 & 66\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
- 63,575
-
You could replace
\midrulewith\cmidrule(l){2-6}to make it more obvious, visually speaking, which columns pertain to "y". – Mico Mar 15 '14 at 09:38 -
If you must use a vertical rule in your table, I would suggest you also insert "struts" to get better vertical spacing above and below the horizontal lines drawn by \hline. In general, though, I find it preferable to construct tables without vertical rules and to use the booktabs package to get well-spaced horizontal lines drawn by \toprule, \midrule, etc.
To vertically center the second entry in the first column, use a \multirow instruction.
You can also automate the job of making the first column always in bold.
Finally, if you want to use the tabularx package, say because you want to achieve a prespecified width of the table, and if you want the contents to be centered (which seems to be the case in the screenshot you posted), you will also need to define a special, "centered" version of the X column type that's provided by the tabularx package.
The first table below uses a tabular environment, and the second uses a tabularx environment with the total width set to 0.7\textwidth. The second table is also set in sans-serif.

\documentclass[10pt,a4paper]{article}
\usepackage{multirow,array}
% define "struts" as suggested by Claudio Beccari in
% a piece in TeX and TUG News, Vol. 2, 1993.
\newcommand\T{\rule{0pt}{2.6ex}} % = `top' strut
\newcommand\B{\rule[-0.9ex]{0pt}{0pt}} % = `bottom' strut
\newcommand\TB{\T\B} % top-and-bottom strut
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{tabular}{>{\bfseries}c|ccccc}
x & \multicolumn{5}{c}{\bfseries y\B}\\
\hline
10 & 22 & 33\TB\\
\hline
\multirow{2}{*}{20} & 45 & 34 & 88 & 77 & 80\T\\
& 34 & 67\B \\
\hline
66 & 12 & 10 & 45 & 66\T\\
\end{tabular}
\bigskip
\textsf{ % switch to sans-serif for the second table
\begin{tabularx}{0.7\textwidth}{>{\bfseries}C|CCCCC}
x & \multicolumn{5}{c}{\bfseries y\B}\\
\hline
10 & 22 & 33\TB\\
\hline
\multirow{2}{*}{20} & 45 & 34 & 88 & 77 & 80\T\\
& 34 & 67\B \\
\hline
66 & 12 & 10 & 45 & 66\T\\
\end{tabularx}}
\end{document}
- 506,678
A solution using, in addition of array, only the makecell package. One advantage is that one can have much less tight rows with the \makecell* command (it adds \jot symmetrically at the top and the bottom of a cell, unlike \renewcommand{\arraystretch}{…}. Another is the possibility to change hlines thickness (like booktabs, but keeping the possibility to use vertical lines).
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array, makecell}
\begin{document}
\begin{table}
\centering\sffamily
\addtolength{\tabcolsep}{10pt}% increase distance between cells
\begin{tabular}{c!{\vline width1pt}*{5}{c}}
\makecell*{x} & \multicolumn{5}{c}{y}\\
\Xhline{1pt}
\makecell*{10} & 22 & 33 \\
\hline
\makecell{20\\[-2.5ex]}& \makecell*[tc]{45\\34} & \makecell[tc]{34\\67} & 88 & 77 & 80 \\
\hline
\makecell*{66} & 12 & 10 & 45 & 66
\end{tabular}
\end{table}
\end{document}

- 271,350
tabularto end the current row with\\at a time where less cells have been filled than in the preamble. – Mar 15 '14 at 08:48multirowwill do it: see the answers. – Mar 15 '14 at 09:15