4

I've made the following table using \multicolumn. Everything looks fine. Unfortunately when I replace A, B or C with some longer text, I get unequal spacing between the three columns below. Can anyone explain me why?

\begin{table}[h!]
  \centering
    \begin{tabular}{|ccc|ccc|ccc|c|}
    \toprule
\multicolumn{3}{|c}{Longer text} & \multicolumn{3}{|c}{B} & \multicolumn{3}{|c}{C} &\multicolumn{1}{|c|}{D} \\
    \midrule 
    a & b & c & a & b & c & a & b & c & \multicolumn{1}{c|}{} \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 & \multicolumn{1}{c|}{4}\\
     a & b & c & a & b & c & a & b & c & \multicolumn{1}{c|}{} \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 & \multicolumn{1}{c|}{}\\
\cline{1-9}
    \multicolumn{3}{|c|}{A} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c|}{C} & \multicolumn{1}{c|}{} \\
\cline{1-9}
     a & b & c & a & b & c & a & b & c & \multicolumn{1}{c|}{} \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 & \multicolumn{1}{c|}{}\\
     a & b & c & a & b & c & a & b & c & \multicolumn{1}{c|}{} \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 & \multicolumn{1}{c|}{}\\
    \bottomrule
    \end{tabular}
\end{table}
2
  • Welcome to TeX.SX. Please post a full example, not just fragments. Apparently, you're using booktabs?
    – user31729
    Commented Dec 28, 2015 at 16:14
  • Tabular environment stretches the space to fit the content. If you don't want this, you should specify the width, for instance with p{1cm} instead of c.
    – Runar
    Commented Dec 28, 2015 at 16:19

2 Answers 2

3

Some propositions: Remove the vertical lines and use a C{0.3cm} etc. columntype in conjunction with the *{3}C{} repetition codes, to ease the use of such column beasts:

\documentclass{article}
\usepackage{array}

\usepackage{booktabs}

\newcolumntype{C}[1]{>{\centering}p{#1}}
\newlength{\stdcolumnwidth}
\setlength{\stdcolumnwidth}{0.3cm}

\begin{document}


\begin{table}[h!]
  \centering
    \begin{tabular}{*{3}{C{\stdcolumnwidth}}*{3}{C{\stdcolumnwidth}}*{3}{C{\stdcolumnwidth}}c}
    \toprule
    \multicolumn{3}{c}{Longer text} & \multicolumn{3}{c}{B} & \multicolumn{3}{c}{C} &D \\
    \midrule 
    a & b & c & a & b & c & a & b & c & \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 & 4\\
    a & b & c & a & b & c & a & b & c & \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 & \\
    \midrule
    \multicolumn{3}{c}{A} & \multicolumn{3}{c}{B} & \multicolumn{3}{c}{C} &  \\
    \midrule
    a & b & c & a & b & c & a & b & c &  \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 &  \\
    a & b & c & a & b & c & a & b & c &  \\
    1 & 2 & 3 & 1 & 2 & 3 & 1 & 2 & 3 &  \\
    \bottomrule
    \end{tabular}
\end{table}

\end{document}

enter image description here

0
1

Your problem arise when the width of text in multicolumn cell is wider than sum of longest text of belonging columns (see figure below). In this case first two columns have natural width, but third one accommodate its width to width requested by multicolum cell.

This problem can be solved on different ways, among them the following two are relative simple, however both require manual tweaking of columns width. In both the table columns are divided into groups (as suggest Christian Hupfer in his answer). With this you have the columns width in the columns, where arise your problem. Proposed solutions are:

  • you adjust width of columns in the group where the your problem arise for amount, that widths of all belonging columns are equal

  • make text in multicolumn cell narrower with breaking it into more lines so, that it can be fit in available width for multicolum cell.

  • of course, both aforementioned approach can be combined in searching a compromise for better looks of table

The problem and first two mentioned solution illustrate MWE below:

\documentclass{article}
    \usepackage{array,booktabs,makecell}
    \usepackage{stackengine}
\renewcommand\theadset{\normalsize\renewcommand\arraystretch{.85}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

    \begin{document}
    \centering
demonstrate the problem\\
\begin{tabular}{|*{3}{C{4mm}|}*{3}{C{4mm}|}*{3}{C{4mm}|}c|}
    \toprule
\multicolumn{3}{|c|}{Longer text}
            &   \multicolumn{3}{c|}{BBBB BBB BB BBBBB}
                            &   \multicolumn{3}{c|}{C}
                                            & D   \\
    \midrule
a & b & c   & a & b & c     & a & b & c     &     \\
1 & 2 & 3   & 1 & 2 & 3     & 1 & 2 & 3     & 4   \\
    \bottomrule
\end{tabular}

\medskip
first possible solution\\
\begin{tabular}{|*{3}{C{4mm}|}*{3}{C{10mm}|}*{3}{C{4mm}|}c|}
    \toprule
\multicolumn{3}{|c|}{Longer text}
            & \multicolumn{3}{c|}{BBBB BBB BB BBBBB}
                            & \multicolumn{3}{c|}{C}
                                            & D   \\
    \midrule
a & b & c   & a & b & c     & a & b & c     &     \\
1 & 2 & 3   & 1 & 2 & 3     & 1 & 2 & 3     & 4   \\
    \bottomrule
\end{tabular}

\medskip
second possible solution\\
\begin{tabular}{|*{3}{C{4mm}|}*{3}{C{4mm}|}*{3}{C{4mm}|}c|}
    \toprule
\multicolumn{3}{|c|}{Longer text}
            & \multicolumn{3}{c|}{\thead[b]{BBBB BBB\\ BB BBBBB}}
                            & \multicolumn{3}{c|}{C}
                                            & D   \\
    \midrule
a & b & c   & a & b & c     & a & b & c     &     \\
1 & 2 & 3   & 1 & 2 & 3     & 1 & 2 & 3     & 4   \\
    \bottomrule
\end{tabular}
    \end{document}

Package makecell is added for second solution, where is used macro thead from this package. For illustration I consider only first three rows from table.

enter image description here

You must log in to answer this question.

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