\multicolumn

Synopsis:

\multicolumn{numcols}{cols}{text}

Make an array or tabular entry that spans several columns. The first argument numcols gives the number of columns to span. The second argument cols specifies the formatting of the entry, with c for centered, l for flush left, or r for flush right. The third argument text gives the contents of that entry.

In this example, in the first row, the second and third columns are spanned by the single heading ‘Name’.

\begin{tabular}{lccl} 
  \textit{ID}       &\multicolumn{2}{c}{\textit{Name}} &\textit{Age} \\
  \hline  
  978-0-393-03701-2 &O'Brian &Patrick                  &55           \\
    ...
\end{tabular}

What counts as a column is: the column format specifier for the array or tabular environment is broken into parts, where each part (except the first) begins with l, c, r, or p. So from \begin{tabular}{|r|ccp{1.5in}|} the parts are |r|, c, c, and p{1.5in}|.

The cols argument overrides the array or tabular environment’s intercolumn area default adjoining this multicolumn entry. To affect that area, this argument can contain vertical bars | indicating the placement of vertical rules, and @{...} expressions. Thus if cols is ‘|c|’ then this multicolumn entry will be centered and a vertical rule will come in the intercolumn area before it and after it. This table details the exact behavior.

\begin{tabular}{|cc|c|c|}
  \multicolumn{1}{r}{w}       % entry one
    &\multicolumn{1}{|r|}{x}  % entry two 
    &\multicolumn{1}{|r}{y}   % entry three
    &z                        % entry four
\end{tabular}

Before the first entry the output will not have a vertical rule because the \multicolumn has the cols specifier ‘r’ with no initial vertical bar. Between entry one and entry two there will be a vertical rule; although the first cols does not have an ending vertical bar, the second cols does have a starting one. Between entry two and entry three there is a single vertical rule; despite that the cols in both of the surrounding multicolumn’s call for a vertical rule, you only get one rule. Between entry three and entry four there is no vertical rule; the default calls for one but the cols in the entry three \multicolumn leaves it out, and that takes precedence. Finally, following entry four there is a vertical rule because of the default.

The number of spanned columns numcols can be 1. Besides giving the ability to change the horizontal alignment, this also is useful to override for one row the tabular definition’s default intercolumn area specification, including the placement of vertical rules.

In the example below, in the tabular definition the first column is specified to default to left justified but in the first row the entry is centered with \multicolumn{1}{c}{\textsc{Period}}. Also in the first row, the second and third columns are spanned by a single entry with \multicolumn{2}{c}{\textsc{Span}}, overriding the specification to center those two columns on the page range en-dash.

\begin{tabular}{l|r@{--}l} 
  \multicolumn{1}{c}{\textsc{Period}}  
    &multicolumn{2}{c}{\textsc{Span}} \\ \hline
  Baroque          &1600           &1760         \\
  Classical        &1730           &1820         \\
  Romantic         &1780           &1910         \\
  Impressionistic  &1875           &1925
\end{tabular}

Although the tabular specification by default puts a vertical rule between the first and second columns, no such vertical rule appears in the first row here. That’s because there is no vertical bar in the cols part of the first row’s first \multicolumn command.

© 2007–2018 Karl Berry
Public Domain Software
http://latexref.xyz/_005cmulticolumn.html