Home > tables > Wrong width in multicolumn tabularx cells

Wrong width in multicolumn tabularx cells

February 6Hits:2
Advertisement

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.

Answers

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:

Wrong width in multicolumn tabularx cells

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}

Related Articles

  • Wrong width in multicolumn tabularx cells

    Wrong width in multicolumn tabularx cellsFebruary 6

    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 corre

  • \multicolumn table cell doesn't fit page width

    \multicolumn table cell doesn't fit page widthMarch 26

    I have this code: \documentclass{article} \usepackage{lipsum} \begin{document} \begin{tabular}{ |c|c| } \hline \multicolumn{2}{|c}{\lipsum[1]}\\ \hline text text text text text text & text text text text\\ \hline \end{tabular} \end{document} It gener

  • Determine width of a milticolumn cell

    Determine width of a milticolumn cellAugust 19

    I am attempting to draw a rectangle within a multicolumn, and would like it to span the width of the cell and be consistant with a similar rectangle drawn in a standard cell. I am struggling to determine the correct length of the cell. Ultimately I w

  • Automatic wrapping of a \multicolumn table cell

    Automatic wrapping of a \multicolumn table cellAugust 19

    I output lots of tables to LaTeX using the estout/esttab package in Stata. I frequently want to have long footnotes on the bottom of the table, explaining coefficients, sources, etc. The estout package inserts footnotes using \multicolumn{#}{l}{...}

  • Width of nested tabularx environments

    Width of nested tabularx environmentsOctober 6

    I am trying to create a RPG "character sheet" using LaTeX. For those unfamiliar with the madness of nested boxes, underlined whitespace etc. involved, gives a good impression (except that I want mine to look better, of course. ;-) ) I started wi

  • Column width table in tabularx environment

    Column width table in tabularx environmentMay 9

    This stuff is probably a bit cluttred, but anyways: The problem I am experiencing is that I would like to make a correlation matrix as the one depicted above, but the width of the maturity column should be smaller, such that all the columns are almos

  • Vertical alignment of a tabularx cell

    Vertical alignment of a tabularx cellApril 2

    I have a tabularx table with: a variable width column X on the left a cell with a nested tabular table on the right Like so: \begin{tabularx}{\textwidth}{ | X | c | } \lipsum[1-1] & \begin{tabular}{rl} test&test\\ test&test\\ test&test\\ t

  • How can I center text vertically in the middle of a multicolumn table cell?

    How can I center text vertically in the middle of a multicolumn table cell?April 24

    When I enter this, why does the 'Type I stuff' and 'Type II stuff' break down into 2-3 lines? How can I have them written in one line without linebreak? When I use c instead of M for the multicolumn, it doesn't center the text in the vertical middle

  • How to fit a Tikz picture into a tabularx cell?

    How to fit a Tikz picture into a tabularx cell?June 30

    I am trying to put a Tikz picture into a cell of a tabularx. The problem is that it doesn't fit in my cell. Here is the code : \documentclass[10pt,a4paper]{article} \usepackage[utf8x]{inputenc} \usepackage{ucs} \usepackage{amsmath} \usepackage{amsfon

  • Align content of tabularx cell at top

    Align content of tabularx cell at topNovember 3

    Is there a way to align cell content of a table at the top? I'm using a tabularx table with X columns which are vertically centered, but I want a single cell of my table to be top aligned. Of course, here is a MWE: \documentclass[BCOR=10mm,DIV=calc]{

  • How not to repeat info for each tabularx cell in row?July 7

    This produces a 2x2 table whose first row is blue with white text and whose second row is white with black text. Although it works fine it does repeat \textcolor{white} in each cell of the row where the cells are to have white text. Is there some way

  • Table width with multicolumnJune 3

    I am producing lots of tables automatically with Stata (perhaps its useful to know that the top, content and bottom of the table are produced separately and then pasted together into one file I \input}. All of these tables have notes at the bottom wh

  • fit tabular width with multicolumns into text width latex

    fit tabular width with multicolumns into text width latexJanuary 8

    So i want to make the following table fit in my text width. The table is much longer than the text. Any propositions? Thanks in advance. Here is the table code (dont pay attention to the comments' lines with %) \begin{table}[htbbp] \caption{Methods p

  • How can I insert complex regular expressions into tabularx cells?

    How can I insert complex regular expressions into tabularx cells?May 29

    I'm trying to create a table with two columns, in which the second one is representing lots of regular expression with all the character you could imagine. I tried with \verb|text| command, but it gets stuck when there are '%' symbols and, if i try t

  • Wrapping column text using multicolumn and tabularxOctober 11

    I have two tables I am typesetting above each other as follows. \documentclass[a4paper,11pt]{article} \usepackage{tabularx} \usepackage[left=2cm, right=2cm, bmargin=1.5cm]{geometry} \begin{document} \begin{tabularx}{\textwidth}{| X | X | l | l | l |

  • How to set equal size of cells in tabular environment when \multicolumn extend the size of above cell?

    How to set equal size of cells in tabular environment when \multicolumn extend the size of above cell?March 28

    The code example is shown as follows: \documentclass{article} \usepackage{latexsym} \usepackage{bm} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsmath} \begin{document} \begin{center} \begin{tabular}{|c|c|c|c|c|c|c|c|c|} \hline \multicolu

  • Set multicolumn width to default table width

    Set multicolumn width to default table widthFebruary 21

    I have a two column table with a single multicolumn spanning both of them. The text on that multicolumn is rather long so I need to set its width so it wraps around. I can set the width with \multicolumn{2}{p{whatever}}{text} but what should I set "w

  • Have a table-cell spanning entire table with automatic width

    Have a table-cell spanning entire table with automatic width February 21

    I have a two column table with a single multicolumn spanning both of them. The text on that multicolumn is rather long so I need to set its width so it wraps around. I can set the width with \multicolumn{2}{p{whatever}}{text} but what should I set "w

  • Know the cell width with tabular* or include a paragraph box without specifying its width

    Know the cell width with tabular* or include a paragraph box without specifying its widthApril 13

    I wrote the following table using tabular* inside a \newenvironment. Worked fine but I prefer to have text wrapping on one column. The table uses "phantom" cells to achive what I want that is having one column which takes all available width tha

  • Automatic new lines in cells in a table to fit page width

    Automatic new lines in cells in a table to fit page widthJune 3

    I'd like to let latex automatically put a new line in cells so to fit the table in the page width, is this possible? For example this code would show the table but the table would not fit in the page width: \usepackage{tabularx} \begin{tabular*}{\tex

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.412 s.