Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I have a table like the following picture in latex? enter image description here

share|improve this question
    
there is no problem in a tabular to end the current row with ` \\ ` at a time where less cells have been filled than in the preamble. –  jfbu Mar 15 at 8:48
    
How can I put 20 in the middle of two corresponding rows?Since my second row is too long, I have to cut it and the rest write in the second row. –  rose Mar 15 at 8:55
    
multirow will do it: see the answers. –  jfbu Mar 15 at 9:15
2  
Rather than splitting a row, I'd suggest to reduce the intercolumn space. Is the alignment between 45 and 34 in the “20 row” significant? –  egreg Mar 15 at 9:53

3 Answers 3

up vote 6 down vote accepted

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.

enter image description here

\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}
share|improve this answer
    
You could replace \midrule with \cmidrule(l){2-6} to make it more obvious, visually speaking, which columns pertain to "y". –  Mico Mar 15 at 9:38
    
@Mico: you're right, thanks! I'm editing the answer. –  Claudio Fiandrino Mar 15 at 9:48

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.

enter image description here

\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}
share|improve this answer
    
Thanks for your reply. –  rose Mar 15 at 10:20

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} 

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.