[Tex/LaTex] Wrong width in multicolumn tabularx cells

multicolumntablestabularx

I'm trying to create a table with fixed column widths. As most rows have a different number of cells (with LCM 12) I defined a 12 column wide table, where I add multicolumns of the wanted size. The problem is, that the sizes are only calculated correctely for the 6 double-column fields. The 12-column captions and the 4 three-column fields are wrong.

Minimal example (including color, to make the problem more obvious):

\documentclass[a4paper,12pt]{book}
\usepackage[table]{xcolor}
\usepackage{tabularx}

\begin{document}

\rowcolors{1}{green}{red}
\newcommand{\mc}[2]{\multicolumn{#1}{|>{\setlength{\hsize}{#1\hsize} }X|}{#2}}
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|X|X|X|X|X|X|}
\mc{12}{Caption 1} \\
\mc{2}{Field 1.1} & \mc{2}{Field 1.2} & \mc{2}{Field 1.3} & \mc{2}{Field 1.4} & \mc{2}{Field 1.5} & \mc{2}{Field 1.6} \\
\mc{12}{Caption 2} \\
\mc{3}{Field 2.1} & \mc{3}{Field 2.2} & \mc{3}{Field 2.3} & \mc{3}{Field 2.4} \\
\end{tabularx}

\end{document}

Where does this comes from and how I can get it to work like it should? Different approaches to the problem with the varying amount of cells are also welcome, the cell width per row has to stay the same for all the rows cells though.

Best Answer

The tabularx documentation states:

Do not use \multicolumn entries which cross any X column.

As with most rules, these may be broken if you know what you are doing.

So, let's try to circumvent these limitations in some way. Let's define a new command

\newcommand{\mcc}[2]{\multicolumn{#1}{|l|}{#2}}

and modify your MWE in this way:

\documentclass[a4paper,12pt]{book}
\usepackage[table]{xcolor}
\usepackage{tabularx}

\begin{document}

\rowcolors{1}{green}{red}
\newcommand{\mc}[2]{\multicolumn{#1}{|>{\setlength{\hsize}{#1\hsize}}X|}{#2}}
\newcommand{\mcc}[2]{\multicolumn{#1}{|l|}{#2}}
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|X|X|X|X|X|X|}
\mcc{12}{Caption 1} \\
\mc{2}{Field 1.1} & \mc{2}{Field 1.2} & \mc{2}{Field 1.3} & \mc{2}{Field 1.4} & \mc{2}{Field 1.5} & \mc{2}{Field 1.6} \\
\mcc{12}{Caption 2} \\
\mc{3}{Field 2.1} & \mcc{3}{Field 2.2} & \mc{3}{Field 2.3} & \mcc{3}{Field 2.4} \\
\end{tabularx}

\end{document} 

Result:

enter image description here

Related Question