I try to use \multirow to create the table below, but it failed. Making the "unit" below the item causes table creation difficult for me. Anyone can help me out?
4 Answers
I don't think that using \multirow
improves the look of the header cells -- quite the opposite, actually. The big gap between the first and second group of three columns seems excessive; you may get a better overall effect by trimming the \cmidrule
s to leave a modest visual gap. Finally, since the data are numeric, you may want to consider using the S
column type (provided by the siunitx
package) instead of the basic c
column type.
\documentclass{book}
\usepackage{booktabs,caption,siunitx}
\begin{document}
\begin{table}
\centering
\caption{Comparison of Groups A and B}\label{AB}
\begin{tabular}{@{}l*{7}{S[table-format=1.3]}@{}}
\toprule
No. & A & \multicolumn{3}{c}{Group A} &\multicolumn{3}{c@{}}{Group B}\\
\cmidrule(lr){3-5}
\cmidrule(l){6-8}
&&A&B&C&A&B&C\\
&{(unit)}&{(unit)}&&{(unit)}&{(unit)}&&{(unit)}\\
\midrule
1 & 0.751 & 0.498 & 0.706 & 0.823 & 0.439 & 0.490 & 0.276\\
2 & 0.255 & 0.960 & 0.032 & 0.695 & 0.382 & 0.446 & 0.680\\
3 & 0.506 & 0.340 & 0.277 & 0.317 & 0.766 & 0.646 & 0.655\\
4 & 0.699 & 0.585 & 0.046 & 0.950 & 0.795 & 0.709 & 0.163\\
5 & 0.891 & 0.224 & 0.097 & 0.034 & 0.187 & 0.755 & 0.119\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Table Construction:
Code:
\documentclass[table]{book}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{caption}
\usepackage{listings}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular*}{0.90\textwidth}{@{\extracolsep{\fill}}rrrrrrrr}
\hline \hline
\multirow{4}{*}{No} & \multirow{4}{*}{A} & \multicolumn{3}{c}{Group A} & \multicolumn{3}{c}{Group B}\\
& \multirow{4}{*}{(unit)}&\multicolumn{3}{c}{-------------------------------} &\multicolumn{3}{c}{-------------------------------} \\
& & A & \quad \quad B & C & A &\quad \quad B &C \\
& & (unit) & &(unit) & (unit) & &(unit)\\
\hline
1 & 0.751 & 0.498 & 0.706 & 0.823 & 0.439 & 0.490 & 0.276 \\
2 & 0.255 & 0.960 & 0.032 & 0.695 & 0.382 & 0.446 & 0.680 \\
3& 0.506 & 0.340 & 0.277 & 0.317 & 0.766 & 0.646 & 0.655 \\
4& 0.699 & 0.585 & 0.046 & 0.950 & 0.795 & 0.709 &0.163 \\
5 & 0.891 & 0.224 & 0.097 & 0.034 & 0.187 & 0.755 & 0.119\\
\hline \hline
\end{tabular*}
\caption{Comparison of Group A and B}
\label{tab:compr}
\end{table}
\end{document}
Result
-
Thanks for your help. It seams that I get a better solution. See below Commented Aug 24, 2014 at 7:35
-
1If you're going to use
------...
to create long lines, be sure to use a number of dashes that's an exact multiple of three. Otherwise you end up with a bunch of em-dashes followed by either a simple dash (as in the screenshot) or an en-dash. That said, either\cline
or\cmidrule
would seem to provide a more natural way for obtaining the lines in question.– MicoCommented Aug 24, 2014 at 9:11
\documentclass[table]{book}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{caption}
\begin{document}
\begin{table}[H]
\centering
\caption{Comparison of Group A and B}\label{AB}
\begin{tabular}{ccccccccc}
\toprule
\multirow{3}[4]{*}{No.} & & \multicolumn{3}{c}{Group A} & &\multicolumn{3}{c}{Group B}\\
\cmidrule(r){3-5}
\cmidrule(r){7-9}
&A&A&\multirow{2}[4]{*}{B}&C&&A&\multirow{2}[4]{*}{B}&C\\
&(unit)&(unit)&&(unit)&&(unit)&&(unit)\\
\midrule
1 & 0.751 & 0.498 & 0.706 & 0.823 && 0.439 & 0.490 & 0.276 \\
2 & 0.255 & 0.960 & 0.032 & 0.695 && 0.382 & 0.446 & 0.680 \\
3 & 0.506 & 0.340 & 0.277 & 0.317 && 0.766 & 0.646 & 0.655 \\
4 & 0.699 & 0.585 & 0.046 & 0.950 && 0.795 & 0.709 &0.163 \\
5 & 0.891 & 0.224 & 0.097 & 0.034 && 0.187 & 0.755 & 0.119\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
-
1please use the '{}' button to format your code as code. also I would consider keeping all the column names on the same row, simply leaving blanks on the unit row where none is necessary - and i have seen that done with additional single-column
cmidrule
s between unit and column name.. Commented Aug 24, 2014 at 7:38 -
1The placement of the
B
column headers strikes me as bordering on the hideous. Loading themulticol
andcaption
packages isn't needed for this MWE, is it? What is the purpose of thetable
option in the\documentclass
command? The[H]
location specifier doesn't do anything unless thefloat
package is loaded as well.– MicoCommented Aug 24, 2014 at 7:44 -
-
@greyshade: Thanks for your suggestion. I will keep all the column names on the same row. Good suggestion! Commented Aug 24, 2014 at 16:50
A different alignment for the first rows, an automatic numbering of rows and a slight simplification of code with the makecell
or stackengine
packages (no multirows):
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{booktabs, caption, siunitx}
\usepackage{makecell}
\renewcommand\theadalign{tc}
\usepackage{tabstackengine}
\setstackEOL{\\}
\newcounter{rowno}
\newcommand\emptycell{\multicolumn{1}{c}{}}
\providecommand*\No{N\textsuperscript{\kern0.06em\underline{\kern-0.06em o\kern-0.068em}}}
\begin{document}
With \texttt{makecell}:
\begin{table}[!h]
\centering
\setcounter{rowno}{0}
\caption{Comparison of Groups A and B}\label{AB}
\begin{tabular}{>{\refstepcounter{rowno}\,\therowno}c*{7}{S[table-format=1.3]}}%{}
\toprule
\emptycell & & \multicolumn{3}{c}{Group A} &\multicolumn{3}{c@{}}{Group B}\\
\cmidrule(lr){3-5} \cmidrule(lr){6-8}
\multicolumn{1}{c}{\thead{\\\No}} & {\thead{A\\}}& {\thead{A\\(unit)}} &{ \thead{B\\}} &{\thead{C\\(unit)}} & {\thead{A\\(unit)}}& {\thead{B}} & {\thead{C\\(unit)}}\\
\cmidrule[\lightrulewidth](lr){1-8}
\addlinespace[1.2ex]
& 0.751 & 0.498 & 0.706 & 0.823 & 0.439 & 0.490 & 0.276\\
& 0.255 & 0.960 & 0.032 & 0.695 & 0.382 & 0.446 & 0.680\\
& 0.506 & 0.340 & 0.277 & 0.317 & 0.766 & 0.646 & 0.655\\
& 0.699 & 0.585 & 0.046 & 0.950 & 0.795 & 0.709 & 0.163\\
& 0.891 & 0.224 & 0.097 & 0.034 & 0.187 & 0.755 & 0.119\\
\bottomrule
\end{tabular}
\end{table}
\vskip 1cm
With \texttt{stackengine}:
\begin{table}[!h]
\centering
\setcounter{rowno}{0}
\caption{Comparison of Groups A and B}\label{AB}
\begin{tabular}{@{}>{\refstepcounter{rowno}\,\therowno}c*{7}{c}}%{}S[table-format=1.3]
\toprule
\emptycell & & \multicolumn{3}{c}{Group A} &\multicolumn{3}{c@{}}{Group B}\\
\cmidrule(lr){3-5} \cmidrule(lr){6-8}
\multicolumn{1}{c}{\stackunder{}{\No}} & {A} & {\stackunder{A}{(unit)}} & {B} &
{\stackunder{C}{(unit)}} & {\stackunder{A}{(unit)}}& {B} & {\stackunder{C}{(unit)}}\\
\addlinespace
\cmidrule[\lightrulewidth](lr){1-8}
\addlinespace[1.2ex]
& 0.751 & 0.498 & 0.706 & 0.823 & 0.439 & 0.490 & 0.276\\
& 0.255 & 0.960 & 0.032 & 0.695 & 0.382 & 0.446 & 0.680\\
& 0.506 & 0.340 & 0.277 & 0.317 & 0.766 & 0.646 & 0.655\\
& 0.699 & 0.585 & 0.046 & 0.950 & 0.795 & 0.709 & 0.163\\
& 0.891 & 0.224 & 0.097 & 0.034 & 0.187 & 0.755 & 0.119\\
\bottomrule
\end{tabular}
\end{table}
\end{document}