I have some tabular data and two column page layout. The number of columns is bigger such that that the table cannot fit into one column. How can I make the table span two columns such that the table is centered. I don't want to stretch it to the whole. Just take up two columns and center it since it is not big enough to take the whole space. Any suggestions?
[Tex/LaTex] Making a table span multiple columns
multicolumn
Related Solutions
Actually, it's the other way round; initially you declare 5 columns and, in the first row, using \multicolumn
you span some cells to occupy to of them.
In the following example, the packages siunitx
and booktabs
were used to improve the table formatting:
\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\noindent\begin{tabular}{lSSSS}
\toprule
& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{Most Mon} \\
\cmidrule(r){2-3}\cmidrule(l){4-5}
Methods & {Time [\si{\second}]} & {RunCount} & {Time [\si{\second}]} & {RunCount} \\
\midrule
C & 12.3 & 5 & 34.6 & 7 \\
D & 1.35 & 5 & 4.93 & 7 \\
\bottomrule
\end{tabular}
\end{document}
Of course, instead of S
you could use any other column type (and deal with possible alignments in other ways) such as c
, l
, r
, or p{<length>}
.
In a comment it has been requested to produce the table with the vertical rules; here it is:
\documentclass{article}
\usepackage{siunitx}
\begin{document}
\noindent\begin{tabular}{| l | S | S | S | S |}
\cline{2-5}
\multicolumn{1}{c|}{}& \multicolumn{2}{c|}{A} & \multicolumn{2}{c|}{Most Mon} \\
\hline
Methods & {Time [\si{\second}]} & {RunCount} & {Time [\si{\second}]} & {RunCount} \\
\hline
C & 12.3 & 5 & 34.6 & 7 \\
D & 1.35 & 5 & 4.93 & 7 \\
\hline
\end{tabular}
\end{document}
The reason why the "Short" headers don't look like they're centered properly is that the instruction
\multicolumn{2}{c}{Short}
centers the string over two columns that are defined as
@{\hskip 0.25in}r@{\hskip 0.05in}r
To get the correct centering of the "Short" headers, you need to write
\multicolumn{2}{@{}c@{\hskip0.25in}}{Short}
The problem with the "LongLongLongLong" header is that it's assigned the column type c
, which doesn't allow line breaks. If you want to keep the overall width of the pair of columns, you must allow one or more line breaks in the header cell. In the code below, I suggest using a centered version of the p
column type.
\documentclass{scrreprt}
\usepackage{booktabs,array,ragged2e}
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}
\centering
\begin{tabular}{ l *{3}{@{\hskip0.25in}r@{\hskip 0.05in}r} }
\toprule
& \multicolumn{2}{@{}c@{\hskip0.25in}}{Short}
& \multicolumn{2}{@{}c@{\hskip0.25in}}{Short}
& \multicolumn{2}{@{\,}C{1cm}}{LongLong\-LongLong}\\
\midrule
A& 535& (97\%)&265&(96\%)& 250&(90\%)\\
B& 409& (72\%) &181&(64\%) &163&(57\%) \\
C& 108&(100\%) &54&(100\%) &54&(100\%)\\
D &208& (93\%)& 98&(88\%)&84&(75\%)\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Best Answer
This should help. It just shows how your code should look like after using Harish's recommendation:
Here's a screenshot of the result.