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.

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:

enter image description here

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): enter image description here

Further questions:

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

Hope someone can help me. Thank you very much.

share|improve this question
1  
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
1  
@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. –  tohecz 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
add comment

1 Answer 1

up vote 6 down vote accepted

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
\caption{What is this table about?}\label{test1}

\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
\caption{What is this table about?}\label{test2}

\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}

enter image description here

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}
\caption{What is this table about?}\label{test1}

\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}
\caption{What is this table about?}\label{test2}

\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}

enter image description here

share|improve this answer
    
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
1  
@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
1  
@A.F. Well, you're right! I copied the same example as above. Sorry. Fixed now. –  egreg Dec 7 '13 at 11:38
show 8 more comments

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.