0

So I would like to produce a table, whereby the heading of each multicolumn represents a certain distance and the sub-columns some numerical measurements associated with it.

\begin{tabular}{cccc}
\centering                %% <-- Move outside tabular
\hrule                    %% <-- It is either \toprule or \hline

\multicolumn{2}{c}{290cm} & \multicolumn{2}{c}{190cm}\\
n & m & n & m\\
\cmidrule(lr){1-2}
\cmidrule(l){3-4}
14 & 100 & 6 & 50 \\
2 & 17 & 5 & 45\\
4 & 30 & & \\
9 & 80 & & \\
\end{tabular}

The problem is for some reason the "290cm" isn't aligned as a multicolumn heading, but as a heading only of the second sub-column. What's wrong?

siracusa
  • 13,411
  • Your example doesn't compile, even with the appropriate markup added around it. \hrule can't be used there; you shouldn't use \centering within your tabular environment; and you should mention that you're using booktabs. All that said, when the \hrule and centering are removed, your table appears precisely as it should, as far as I can tell, including the "290cm" being centered over its two columns. What precisely is going wrong? – dgoodmaniii Nov 23 '19 at 20:05
  • Please always post a complete small document that peopel can run to debug your problem without having to guess the definition of commands used. (\cmidrule is not a standard command for example) – David Carlisle Nov 23 '19 at 20:06
  • 190 cm is wider than the combined widths of the first and second column. The multicolumn entry will therefore appear os off center with respect to the corresponding columns. See also Table column widths disproportionate due to multicolumn cell being too long – leandriis Nov 23 '19 at 20:14
  • Yes I'm sorry; this is my very first post on this exchange forum. I will include those for future questions. – Amin Fard Nov 23 '19 at 21:01
  • @leandriis I compiled the tabular with tablary and four C columns, the width set to 107pt (not overfull boxes) and tymin 1pt. Both tabulars was exactly the same. The off center is probably only an optical illusion because the columns have different width. – Sveinung Nov 23 '19 at 21:03
  • It was indeed a problem of proper compilation. Once that was solved then the alignments became proper. Thanks! – Amin Fard Nov 23 '19 at 21:09

2 Answers2

2

For me it seems that it compiles correctly, if you move the \centering out of the tabular and change \hrule to \toprule. However, if you use four c-columns, the second column is wider than the other, and the third column is a little narrower. Therefore, the second heading (190cm) seems to be a little bit offset, but I think this is just an optical illusion. Maybe you should consider using four fixed width columns, as I do in the test example.

In the test example, I have set the tabular with fixed width columns (wc{<wd>}), to demonstrate that the alignment is correct. For the same reason, I added the vertical lines. They are there for illustration purposes and should be removed:

Example 1 – test version

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}

\begin{document}
\centering

\begin{tabular}{*{4}{wc{1cm}|}}
\toprule
\multicolumn{2}{c}{290cm} & \multicolumn{2}{c}{190cm}\\
n & m & n & m\\
\cmidrule(lr){1-2}
\cmidrule(l){3-4}
14 & 100 & 6 & 50 \\
2 & 17 & 5 & 45\\
4 & 30 & & \\
9 & 80 & & \\
\end{tabular}
\end{document}

Example 2 – final version A

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}

\begin{document}
\centering

%\begin{tabular}{*{4}{wc{1cm}|}}
\begin{tabular}{*{4}{c}}
\toprule
\multicolumn{2}{c}{290cm} & \multicolumn{2}{c}{190cm}\\
n & m & n & m\\
\cmidrule(r){1-2}
\cmidrule(l){3-4}
14 & 100 & 6 & 50 \\
2 & 17 & 5 & 45\\
4 & 30 & & \\
9 & 80 & & \\
\bottomrule
\end{tabular}
\end{document}

Example 3 – final version B

Here is a version with fixed width columns. 15 pt in minimum width before you get Overfull hboxes:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{array, caption}

\begin{document}

\begin{table}
\caption{A table\label{tab:a-table}}
\centering
\begin{tabular}{*{4}{Wc{15pt}}}
\toprule
\multicolumn{2}{c}{290cm} & \multicolumn{2}{c}{190cm}\\
\multicolumn{1}{c}{n} & \multicolumn{1}{c}{m} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{m}\\
\cmidrule(r){1-2}
\cmidrule(l){3-4}
14 & 100 & 6 & 50 \\
2   &  17  & 5 & 45\\
4   &  30  &    &     \\
9   &  80  &    &     \\
\bottomrule
\end{tabular}
\end{table}

\end{document}
Sveinung
  • 20,355
0

Use also siunitx for better aligning the figures.

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

\begin{document}

\begin{tabular}{
  S[table-format=2.0]
  S[table-format=3.0]
  S[table-format=2.0]
  S[table-format=2.0]
}
\toprule
\multicolumn{2}{c}{\SI{290}{cm}} &
\multicolumn{2}{c}{\SI{190}{cm}} \\
\cmidrule(lr){1-2} \cmidrule(lr){3-4}
{$n$} & {$m$} & {$n$} & {$m$} \\
\midrule
14 & 100 & 6 & 50 \\
2 & 17 & 5 & 45\\
4 & 30 & & \\
9 & 80 & & \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712