5

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.

1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Feb 6, 2014 at 22:17

3 Answers 3

7

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

1
  • Unfortunately with your solution the Fields 2.1 and 2.3 are too short, while 2.2 and 2.4 are too long. It made me realize, where the problem came from though and I was able to develop an acceptable solution myself. So thanks alot.
    – hakai
    Feb 7, 2014 at 1:40
0

I was able to develop a working solution myself now. Not pretty, but does what it should. Basically, I just put an empty line at the bottom, which forces the columns to a specific width, that the multicolumns can refer to.

[EDIT] Changed the line height of the empty line to ~ zero, might be different values than -5mm in different environments though [/EDIT]

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

\begin{document}

\rowcolors{1}{green}{red}
\newcommand{\mc}[2]{\multicolumn{#1}{|c|}{#2}}
\begin{tabularx}{\textwidth}{XXXXXXXXXXXX}
\showrowcolors
\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} \\
\hiderowcolors &&&&&&&&&&&\\[-5mm]
\end{tabularx}

\end{document}
1
  • Your solution doesn't work when there are vertical lines in the table. If your format is |X|X|..|X| instead, the last "hidden" row would still show its vertical lines.
    – Shahbaz
    Jan 26, 2016 at 22:38
0

In {NiceTabularX} of nicematrix, you can create blocks (with the command \Block) which cross the X columns.

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

\begin{document}

\begin{NiceTabularX}{\textwidth}{XXXXXXXXXXXX}[vlines]
\Block{1-*}{Caption 1} \\
\Block{1-2}{Field 1.1} && \Block{1-2}{Field 1.2} && \Block{1-2}{Field 1.3} && \Block{1-2}{Field 1.4} && \Block{1-2}{Field 1.5} && \Block{1-2}{Field 1.6} &\\*
\Block{1-*}{Caption 2} \\
\Block{1-3}{Field 2.1} &&& \Block{1-3}{Field 2.2} &&& \Block{1-3}{Field 2.3} &&& \Block{1-3}{Field 2.4} \\
\end{NiceTabularX}

\end{document}

Output of the above code

You must log in to answer this question.

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