About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.


4.6.2 Spanning Columns

You may have noticed I omitted the column headers in the above example. [Merging cells in a column of a table] The problem with rewriting the table using r@{.}l to align the decimal point is that the header now needs to span the last two columns. This can be done using the command:

\multicolumn{<cols spanned>}{<col specifier>}{<text>}

The first mandatory argument <cols spanned> is the number of columns you want to span, the second argument <col specifier> indicates how to align this column-spanning entry, the third argument <text> indicates what should go in this entry. Note that <col specifier> should only have a single column specifier, such as {c} or {r}. We can use \multicolumn to modify an earlier example as follows:

\begin{tabular}{lrr}
        & \multicolumn{2}{c}{Price (\pounds)}\\
  Item  & ex VAT & inc VAT\\
  Video &   8.99 &  10.56 \\
  CD    &   9.11 &  10.70 \\
  DVD   &  15.00 &  17.63 \\
  \bfseries Total &  33.10 &  39.89
\end{tabular}

Output:

Image showing typeset output (click here for a more detailed description).

Here we are spanning two columns, so the first argument to \multicolumn is {2}, we want the entry centred, so the second argument is {c} and the text to go in this entry is simply {Price (\pounds)}.

[How to alter the alignment of tabular cells]The \multicolumn command can also be used to override the alignment of individual entries. Consider the following example:

\begin{tabular}{lrr}
          & Year1 & Year2 \\
Travel    & 100,000 & 110,000\\
Equipment & 50,000 & 60,000
\end{tabular}

Output:

Image showing typeset output (click here for a more detailed description).

In this example, the headers “Year1” and “Year2” would look better centred, but the rest of the entries in the second and third columns look best right aligned. We can use \multicolumn to span just one column, and use the second argument of \multicolumn to override the column specification:

\begin{tabular}{lrr}
          & \multicolumn{1}{c}{Year1}
          & \multicolumn{1}{c}{Year2}\\
Travel    & 100,000 & 110,000\\
Equipment & 50,000 & 60,000
\end{tabular}

Output:

Image: as the previous example except that the words
'Year1' and 'Year2' have been centred in their columns


This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-00-5).

© 2012 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs