3

I've been trying to format a table without success. I've followed several suggestions that I found in other answers but never manage to fully adapt it.

This is one of the examples I tried:

\documentclass{article}
\usepackage{booktabs, tabularx}
\begin{document}
\begin{center}
\begin{tabularx}{\linewidth}{cr*{6}{c}}
\toprule    
Probe & MW \newline (g/mol) & R \newline (nm) & \multicolumn{4}{c}{V (mL/g)} & K \newline (--) \\ 
\cmidrule(lr){4-7}
    &  &   & 1 & 2 & 3 & avg. &   \\
\midrule
    Label 1&   400        &   0.00      &   0.00     &  2.34    & 12.00  & 1.32 $\pm$ 0.42  & 1.00  \\
    Label 2&   10000      &   1.50      &   0.99     &  3.00    & 15.00  & 1.32 $\pm$ 0.42  & 1.00  \\
    Label 3&   500000     &   18.45     &   0.99     &  2.00    & 20.00  & 1.32 $\pm$ 0.42  & 1.00  \\
\bottomrule
\end{tabularx}
\end{center}
\end{document}

A things to be fixed but I haven't been able to:

  • The names in the header should be centered vertically and I'd prefer the units in a different line.
  • There seems to be an empty column at the end?
  • bonus: How can I add a footnote to make a comment on one value? tried adding \footnotesize{} but the table and the footnote end up in different places.

Overall I just want a decent and simple graph but can't see h

  • The "empty column at the end" phenomenon is not really an empty column. Instead, it's an artifact of the failure to employ one or more columns of type X. – Mico Feb 14 at 17:40
2

You don't need tabularx, which is the wrong tool for a numeric table. Use tabular* and siunitx facilities.

I provide two realizations, the second one without extending the table up to the margins, which seems better.

\documentclass{article}
\usepackage{booktabs,siunitx}

\sisetup{separate-uncertainty}

\begin{document}

\begin{table}[htp]

\caption{Something that describes the table}\label{whatever}

\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\linewidth}{
 @{\extracolsep{\fill}}
 l
 S[table-format=6.0,group-four-digits]
 S[table-format=2.2]
 S[table-format=1.2]
 S[table-format=1.2]
 S[table-format=2.2]
 S[table-format=1.2(2)]
 S[table-format=1.2]
}
\toprule
Probe & {MW} & R & \multicolumn{4}{c}{V (\si{mL/g})} & {K} \\ 
\cmidrule{4-7}
& {(\si{g/mol})} & {(\si{nm})} & {1} & {2} & {3} & {avg.} & {(--)} \\ 
\midrule
Label 1 &    400 &  0.00 & 0.00 & 2.34 & 12.00 & 1.32 \pm 0.42 & 1.00  \\
Label 2 &  10000 &  1.50 & 0.99 & 3.00 & 15.00 & 1.32 \pm 0.42 & 1.00  \\
Label 3 & 500000 & 18.45 & 0.99 & 2.00 & 20.00 & 1.32 \pm 0.42 & 1.00  \\
\bottomrule
\end{tabular*}

\end{table}

\begin{table}[htp]
\centering

\caption{Something that describes the table}\label{whatever2}

\begin{tabular}{
 @{}
 l
 S[table-format=6.0,group-four-digits]
 S[table-format=2.2]
 S[table-format=1.2]
 S[table-format=1.2]
 S[table-format=2.2]
 S[table-format=1.2(2)]
 S[table-format=1.2]
 @{}
}
\toprule
Probe & {MW} & R & \multicolumn{4}{c}{V (\si{mL/g})} & {K} \\ 
\cmidrule{4-7}
& {(\si{g/mol})} & {(\si{nm})} & {1} & {2} & {3} & {avg.} & {(--)} \\ 
\midrule
Label 1 &    400 &  0.00 & 0.00 & 2.34 & 12.00 & 1.32 \pm 0.42 & 1.00  \\
Label 2 &  10000 &  1.50 & 0.99 & 3.00 & 15.00 & 1.32 \pm 0.42 & 1.00  \\
Label 3 & 500000 & 18.45 & 0.99 & 2.00 & 20.00 & 1.32 \pm 0.42 & 1.00  \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

You can center the header in the first column by using \multicolumn{1}{c}{Probe} (or \multicolumn{1}{@{}c}{Probe} for the second realization, where we remove the padding on the left of the first column).

I added also a trick for pulling the units nearer the header.

\documentclass{article}
\usepackage{amsmath,booktabs,siunitx}

\sisetup{separate-uncertainty}

\newcommand{\tabhead}[1]{%
  \smash[b]{%
    \begin{tabular}[t]{@{}c@{}}#1\end{tabular}%
  }%
}

\begin{document}

\begin{table}[htp]

\caption{Something that describes the table}\label{whatever}

\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\linewidth}{
 @{\extracolsep{\fill}}
 l
 S[table-format=6.0,group-four-digits]
 S[table-format=2.2]
 S[table-format=1.2]
 S[table-format=1.2]
 S[table-format=2.2]
 S[table-format=1.2(2)]
 S[table-format=1.2]
}
\toprule
\multicolumn{1}{c}{Probe} &
{\tabhead{MW \\ (\si{g/mol})}} &
{\tabhead{R \\ (\si{nm})}} &
\multicolumn{4}{c}{V (\si{mL/g})} &
{\tabhead{K \\ (--)}} \\ 
\cmidrule{4-7}
&&& {1} & {2} & {3} & {avg.} \\
\midrule
Label 1 &    400 &  0.00 & 0.00 & 2.34 & 12.00 & 1.32 \pm 0.42 & 1.00  \\
Label 2 &  10000 &  1.50 & 0.99 & 3.00 & 15.00 & 1.32 \pm 0.42 & 1.00  \\
Label 3 & 500000 & 18.45 & 0.99 & 2.00 & 20.00 & 1.32 \pm 0.42 & 1.00  \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

enter image description here

  • 1
    I guess there might still be a rationale for using a tabularx rather than a tabular* environment: If the entries in the first column are a bit more descriptive, and a lot longer, than Label 1, Label 2, etc., it would be useful to assign the X column type to the first column and thus enable automatic line wrapping. At least, I would fervently hope that the real table's headers are a bit more informative than "Label 1", "Label 2", etc are... – Mico Feb 14 at 21:53
  • Thank you! didn't know the difference between tabular and tabularx. Any ideas how to center probe in the middle and make the other labels closer to the units? I'd like to reduce the white space between them. – egil137 Feb 15 at 9:28
  • @egil137 I wouldn't center the header, but it's a stylistic choice. I added the other trick you asked for. – egreg Feb 15 at 9:37
  • Looks great! I have a new problem though :/ I'm working with a template and noticed that this line is preventing the correct spacing so it's preventing \tabhead to work: \renewcommand{\baselinestretch}{1.5}. I would be afraid of removing this line and affecting some other formatting, or would this only affect tables? – egil137 Feb 15 at 10:06
  • @egil137 Add \renewcommand{\arraystretch}{0.66667} in your document preamble, which will remove the stretching in tables. – egreg Feb 15 at 10:09
2

Some suggestions:

  • When using the tabularx environment, make sure to use at least one column of type X. For the table at hand, it seems OK to change the type of the first column from l to X. I'm assuming (hoping?!) that the "real" column labels will be more descriptive, and thus possibly also longer, than "Label 1", "Label 2", and "Label 3". If that's the case, it's useful to use the X column type for the first column as it allows automatic linebreaking. (The l column type does not.) In contrast, there would appear to be no scope for allowing line breaks in any of the other columns.

    The appearance of what looks like an empty column at the right-hand edge of your code is caused directly by the failure to use one or more columns of type X.

  • As you've discovered, using \newline in a cell doesn't work. In the answer below, I propose a handy macro to typeset a header cell and associated units in a stacked format.

  • Optional: Consider using the siunitx package to (a) typeset scientific units properly (via he package's \si macro) and (b) align the numbers in columns 2 and 3 on their (explicit or implicit) decimal markers (via the package's S column type).

enter image description here

\documentclass{article}
\usepackage{booktabs, tabularx, siunitx, xcolor}
\sisetup{per-mode=symbol,group-digits=false}

%% handy utility macro:
\newcommand\smalltab[1]{%
    \smash{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}}

\begin{document}

\noindent
\textcolor{red}{Before}

\begin{center}
\begin{tabularx}{\linewidth}{@{} l r *{6}{c}}
\toprule    
Probe & MW \newline (g/mol) & R \newline (nm) & \multicolumn{4}{c}{V (mL/g)} & K \newline (--) \\ 
\cmidrule(lr){4-7}
    &  &   & 1 & 2 & 3 & avg. &   \\
\midrule
    Label 1&   400        &   0.00      &   0.00     &  2.34    & 12.00  & 1.32 $\pm$ 0.42  & 1.00  \\
    Label 2&   10000      &   1.50      &   0.99     &  3.00    & 15.00  & 1.32 $\pm$ 0.42  & 1.00  \\
    Label 3&   500000     &   18.45     &   0.99     &  2.00    & 20.00  & 1.32 $\pm$ 0.42  & 1.00  \\
\bottomrule
\end{tabularx}
\end{center}

\bigskip\noindent
\textcolor{red}{After}

\begin{center}
\begin{tabularx}{\linewidth}{@{} X
   S[table-format=6.0] S[table-format=2.2] *{5}{c} @{}}
\toprule    
Probe & \smalltab{MW\\(\si{\gram\per\mole})}
      & \smalltab{R\\(\si{\nano\meter})} 
      & \multicolumn{4}{c}{V (\si{\milli\liter\per\gram})} 
      & \smalltab{K\\(--)} \\ 
\cmidrule(lr){4-7}
& & & 1 & 2 & 3 & avg. & \\
\midrule
Label 1 & 400    & 0.00  & 0.00 &  2.34 & 12.00  & 1.32\,${\pm}$\,0.42  & 1.00  \\
Label 2 & 10000  & 1.50  & 0.99 &  3.00 & 15.00  & 1.32\,${\pm}$\,0.42  & 1.00  \\
Label 3 & 500000 & 18.45 & 0.99 &  2.00 & 20.00  & 1.32\,${\pm}$\,0.42  & 1.00  \\
\bottomrule
\end{tabularx}
\end{center}

\end{document}
  • Regarding your "bonus" question: I suggest you look into the threeparttable package, its \tnote macro, and its tablenotes environment. – Mico Feb 14 at 17:39
  • Thanks for the suggestions! I hadn't noticed the misalignment in column 2. Unfortunately for some reason I can't compile it in my document. Apparently there's an error with siunitx but I don't know why yet, Also thanks for pointing out the distnction with tabularx. I won't need it in this case but didn't know any better and just borrowed it from the code I had found. My bad! – egil137 Feb 15 at 9:26
  • @egil137 - You mention experiencing "an an error with siunitx". What kind of error? – Mico Feb 15 at 9:36
  • well there are a few error first pointing for a missing or extra } inserted (which I don't think there is or haven't found) and the last error says: "Package siunitx Error: Invalid numerical input '='. \end{tabularx}" I believe it might have to do with a conflicting package in my document? there are many, tried to "debug" but haven't found the culprit. – egil137 Feb 15 at 10:14
  • Did you encase non-numeric cell contents in curly braces? – Mico Feb 15 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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