3

I've created a LaTeX table (using tabular)

\documentclass{article}
\begin{document}
\begin{tabular}{   c |  c  | c    | c| c  }
Heading 1 & \multicolumn{2}{c}{Heading 2} & \multicolumn{2}{c}{Heading 3} \\
1 & 2 & 3 &4 & 5\\
6 & 7 & 8 &9&10\\
\end{tabular}
\end{document}

but am having a few problems:

  • there is no vertical line between Heading 2 and Heading 3
  • the alignment in the 2nd and 3rd column is not centered under 'Heading 2' (and the 4th and 5th column under 'Heading 3')
  • the width of the 2nd and 3rd columns (and 4th and 5th) is not equal

Why is this? Any help with this would be great.

2
  • 1
    This is the intended behavior. The multicolumn definition has its own column specification which overrides the table specification for that row. Write \multicolumn{2}{|c|}{Heading 2} to get a vertical line between the headings 2 and 3.
    – Harald
    Commented Dec 3, 2014 at 15:15
  • You can find some tips on good styling of tabular material in the documentation of package booktabs.
    – Johannes_B
    Commented Dec 3, 2014 at 15:17

2 Answers 2

3

You need c| to put the rule back at the end of the spanning entry.

Harder is the centering. If a spanning cell is wider than the cells it spans, all excess width goes in the last column spanned.

One way to avoid that is to make the columns naturally wider, here I use dcolumn and padding them as if of the form 1234.123 the alignment also makes 10 and 5 line up possibly correctly.

enter image description here

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}{D{.}{.}{4.3}}
\begin{document}
\begin{tabular}{   d |  d  | d    | d| d  }
\multicolumn{1}{c|}{Heading 1}&
 \multicolumn{2}{c|}{Heading 2} &
\multicolumn{2}{c}{Heading 3} \\
1 & 2 & 3 &4 & 5\\
6 & 7 & 8 &9&10\\
\end{tabular}
\end{document}
1
  • Thank you, this works. It would be nice if the columns could be automatically be re-sized (without the need for dcolumn but this works for now.
    – James
    Commented Dec 9, 2014 at 9:54
2

With {NiceTabular} of nicematrix, you don't have to put again the vertical rules in the \multicolumn (by design).

Moreover, you can fix the width of all the columns with the key columns-width.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}

\begin{NiceTabular}{c|c|c|c|c}[columns-width=15mm]
Heading 1 & \multicolumn{2}{c}{Heading 2} & \multicolumn{2}{c}{Heading 3} \\
1 & 2 & 3 &4 & 5  \\
6 & 7 & 8 &9 & 10 \\
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .