Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider a table with several columns. Three consecutive columns are thin. These three thin columns have a long \multicolumn{c}{3}{??} heading over them. If nothing is done to specify how these should be spaced then the default behaviour is to make the first two columns as thin as they can be to accommodate the contents of the first two columns and the final column is made up to the size required to line up with the end of the heading.

One solution, is to manually add space between columns using @{} or manually specify the width of columns using p{??} or S[table-column-width=??] (in the case of the siunitx package).

I was hoping there would be some kind of automated solution, or that this could be a possible development project. It has occurred to me thought that in the more general case, where there might be multiple overlapping \multicolumn commands on different lines then this might range from awkward to impossible.

Here is some code to produce a table with the problem (first) and a slightly awkward solution the problem that I implemented (second). I used the siunitx package to implement columns as I prefer the layout and it provides me with some more options.

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


\begin{document}


\begin{table}
   \centering
%  \sisetup{table-alignment=center,table-column-width=2.8cm}
   \sisetup{table-alignment=center}
   \begin{tabular}{ S S S S }
   \toprule
      \multicolumn{3}{c}{k-points in lattice vector and here's some more words} & \\
      $\mathbf{a}$  &  $\mathbf{b}$  &  $\mathbf{c}$  &  {total k-points}\\
   \midrule
          1             &  1             &  1             &   1    \\
          2             &  1             &  2             &   4    \\
          3             &  2             &  3             &  18    \\
          4             &  2             &  4             &  32    \\
          5             &  3             &  5             &  75    \\
          5             &  4             &  5             & 100    \\
          6             &  4             &  6             & 144    \\
          7             &  5             &  7             & 245    \\
          8             &  6             &  8             & 384    \\
         10             &  6             & 10             & 600    \\
         10             &  8             & 10             & 800    \\
      \bottomrule
   \end{tabular}
\end{table}






\begin{table}
   % try to determine correct column spacing for 3 thin columns under a long heading.
   \def\tempHeading{k-points in lattice vector and here's some more words} % this is the heading
   \newlength{\tempHeadingLen}                          % a length is a type of object and must be defined
   \settowidth{\tempHeadingLen}{\tempHeading}           % get length of heading
   \addtolength{\tempHeadingLen}{-4\tabcolsep}          % 2x2\tabcolsep betweeen columns
   \setlength{\tempHeadingLen}{0.33333\tempHeadingLen} % divide by three. nice if this could be automated.
%
   \centering
   \sisetup{table-alignment=center}
      \begin{tabular}{ S[table-column-width=\tempHeadingLen] 
                       S[table-column-width=\tempHeadingLen]
                       S[table-column-width=\tempHeadingLen]
                       S }
      \toprule
         \multicolumn{3}{c}{\tempHeading} & \\
         $\mathbf{a}$  &  $\mathbf{b}$  &  $\mathbf{c}$  &  {total k-points}\\
      \midrule
            1             &  1             &  1             &   1    \\
            2             &  1             &  2             &   4    \\
            3             &  2             &  3             &  18    \\
            4             &  2             &  4             &  32    \\
            5             &  3             &  5             &  75    \\
            5             &  4             &  5             & 100    \\
            6             &  4             &  6             & 144    \\
            7             &  5             &  7             & 245    \\
            8             &  6             &  8             & 384    \\
            10            &  6             & 10             & 600    \\
            10            &  8             & 10             & 800    \\
      \bottomrule
   \end{tabular}
\end{table}


\end{document}
share|improve this question
Instead of having a \multicolumn header spanning the three data columns, why not having only one column, with the first row containing the header, and the second row being a \multirow containing a nested table with three columns? – DevSolar Aug 23 '11 at 19:45

2 Answers

Use a combination of your example (with the siunitx package) and Gonzalo's answer:

\documentclass{article}
\usepackage{siunitx}% http://ctan.org/pkg/siunitx
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{booktabs}% http://ctan.org/pkg/booktabs

\newcolumntype{L}{>{\centering}X}% Centered fully stretched column

\begin{document}
\begin{table}
   \centering
   \sisetup{table-alignment=center,table-figures-decimal=0}
   \begin{tabularx}{\linewidth}{%
     S[table-figures-integer=2]%
     S[table-figures-integer=1]%
     S[table-figures-integer=2]%
     S[table-figures-integer=3]%
    }
   \toprule
      \multicolumn{3}{c}{$k$-points in lattice vector and here's some more words} & \\
      \multicolumn{1}{L}{$\mathbf{a}$}  &  \multicolumn{1}{L}{$\mathbf{b}$}  &  
      \multicolumn{1}{L}{$\mathbf{c}$}  &  \multicolumn{1}{L}{total $k$-points}\\
   \midrule
          1             &  1             &  1             &   1    \\
          2             &  1             &  2             &   4    \\
          3             &  2             &  3             &  18    \\
          4             &  2             &  4             &  32    \\
          5             &  3             &  5             &  75    \\
          5             &  4             &  5             & 100    \\
          6             &  4             &  6             & 144    \\
          7             &  5             &  7             & 245    \\
          8             &  6             &  8             & 384    \\
         10             &  6             & 10             & 600    \\
         10             &  8             & 10             & 800    \\
      \bottomrule
   \end{tabularx}
\end{table}
\end{document}

The use of \multicolumn{1}{X}{...} in the table header allows for the last column to also stretch as a column of type X, yet still maintain the S column alignment from siunitx.

Table with X (tabularx) and S (siunitx) columns

For each of the columns you are required to specify the (maximum) number of digits it will hold via the table-figures-integer key value. Additionally, since all columns carry integers, table-figures-decimal=0 and table-alignment=center is set globally for the table via \sisetup.

share|improve this answer

You could use the tabularx package:

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

\begin{document}

\begin{table}
   \centering
   \begin{tabularx}{\linewidth}{*{4}{>{\centering\arraybackslash}X}}
   \toprule
      \multicolumn{3}{c}{k-points in lattice vector and here's some more words} & \\
      $\mathbf{a}$  &  $\mathbf{b}$  &  $\mathbf{c}$  &  {total k-points}\\
   \midrule
          1             &  1             &  1             &   1    \\
          2             &  1             &  2             &   4    \\
          3             &  2             &  3             &  18    \\
          4             &  2             &  4             &  32    \\
          5             &  3             &  5             &  75    \\
          5             &  4             &  5             & 100    \\
          6             &  4             &  6             & 144    \\
          7             &  5             &  7             & 245    \\
          8             &  6             &  8             & 384    \\
         10             &  6             & 10             & 600    \\
         10             &  8             & 10             & 800    \\
      \bottomrule
   \end{tabularx}
\end{table}

\end{document}

The resulting table:

Table

EDIT: the tabularx manual recommends not to use \multicolumn entries which cross any X column; in this case, however, there's no apparent problem.

share|improve this answer
Thanks for this solution. I'm rather unfamiliar with the tabularx package. This seems to solve the problem to a certain degree. The problem is that I'd like the numbers aligned on the units, with the column of the numbers themselves to be aligned in the center of the X or S column. This is one of the features of the `siunitsx' package. In your example, all the numbers are centered. – aghsmith May 25 '11 at 19:38
@user1269: then please post a more "real" example code using units; your example code only shows numbers. – Gonzalo Medina May 25 '11 at 19:50
Apologies, a slip of the keyboard. By units I meant 0->9 or the rightmost digit in each number. Basically I wanted right alignment of the numbers relative to one another, with the resultant column of numbers appearing centered within the table column. – aghsmith May 26 '11 at 11:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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