Serious problems with tabularx table layout

I have some issues when creating my tables in LaTeX and really hope, that some of you could help me. My table looks like this:

Here is my coding:

\documentclass[a4paper,12pt,oneside]{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows,positioning,calc}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow,tabularx,booktabs}
\begin{document}

\begin{center}
\begin{tabularx}{0.8\linewidth}{l !{\qquad} r X r X r X r}
\toprule
& \multicolumn{3}{c}{Name1} & & \multicolumn{3}{c}{Name2}
\\ \cmidrule{2-4} \cmidrule{6-8}
& Factor1 && Factor2 && Factor1 && Factor2\\
\midrule
Test 1 & 12 && 32 && 3 && 5 \\
Test 2 & 1234 && 234 && 334 && --- \\
\bottomrule
\end{tabularx}
\end{center}

\end{document}


If it is possible, I would like my table to look like this (there should, of course, still be a space in the line below Name 1 and Name 2):

Further questions:

1. How do I add "Note:"?
2. How do I add table name?

Hope someone can help me. Thank you very much.

• your code doesn't compile for several reasons however you are not saying it doesn't so I am not entirely sure what your problem actually is. for information, with the code provided: you are trying to use tabularx but are loading the tabu package; you are using toprule/(c)midrule/bottomrule without loading the booktabs package. – ArTourter Dec 5 '13 at 12:09
• I forgot some of my preamble. Is it a problem, when loading both tabu and tabularx? – A. F. Dec 5 '13 at 12:22
• @A.F. No, not exactly a problem, they should be able to work next to each other. It was just strange that you didn't load tabularx and you use it. – yo' Dec 5 '13 at 12:27
• Yes. That is right. I just forgot to c/p it. Do you have an idea to my problem? :-) – A. F. Dec 5 '13 at 12:33

I would recommend not widening a tabular beyond its natural width. With tabular* you can avoid “phantom columns”, if you really want to enlarge the intercolumn spaces.

With facilities from siunitx it's easy to make tables containing numeric data.

\documentclass[a4paper]{article}
\usepackage{siunitx,booktabs}
\begin{document}

% natural width
\begin{table}[htp]
\centering

\medskip

\begin{tabular}{
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 &   12 &  32 &   3 & 5     & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\multicolumn{6}{@{}l@{}}{\strut\footnotesize Note: Blah blah}
\end{tabular}
\end{table}

% enlarged width
\begin{table}[htp]
\centering

\medskip

\begin{tabular*}{.8\textwidth}{
@{\extracolsep{\fill}}
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 &   12 &  32 &   3 & 5     & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\multicolumn{6}{@{}l@{}}{\strut\footnotesize Note: Blah blah}
\end{tabular*}
\end{table}

\end{document}


For longer notes that need to be split across lines, the best is to load the threeparttable package.

\documentclass[a4paper]{article}
\usepackage{siunitx,booktabs,threeparttable}

% customize threeparttable
\renewcommand{\TPTnoteSettings}{\footnotesize}
\renewcommand{\TPTnoteLabel}[1]{#1}

\begin{document}

% natural width
\begin{table}[htp]
\centering

\begin{threeparttable}

\medskip

\begin{tabular}{
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 &   12 &  32 &   3 & 5     & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\item[Note:] A long note that goes beyond the table width
because we want to have many many words that
mean as little as possible
\end{tablenotes}
\end{threeparttable}
\end{table}

% enlarged width
\begin{table}[htp]
\centering
\begin{threeparttable}

\medskip

\begin{tabular*}{.8\textwidth}{
@{\extracolsep{\fill}}
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 &   12 &  32 &   3 & 5     & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\end{tabular*}
\begin{tablenotes}
\item[Note:] A long note that goes beyond the table width
because we want to have many many words that
mean as little as possible
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


• Thank you very much. Egreg how do I add more space between lines, as you can see on my example? – A. F. Dec 5 '13 at 13:59
• @A.F. You might make \arraystretch larger, but my opinion is that larger spaces between rows don't improve the table. – egreg Dec 5 '13 at 15:06
• Hi Egreg. I have a problem, when I write a long line in Notes, it would not break in two, so it continue outside the paper. How can I fix that? – A. F. Dec 6 '13 at 17:51
• @A.F. Do you know the threeparttable package? – egreg Dec 6 '13 at 17:52
• @A.F. Well, you're right! I copied the same example as above. Sorry. Fixed now. – egreg Dec 7 '13 at 11:38