Graphics, Figures & Tablestabularx and multicolumn expansion

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

tabularx and multicolumn expansion

Post by ogtifs »

I am trying to use LaTeX to produce an auto-sizing table similar to that in HTML. My specific problem is using \multicolumn to span multiple columns - the content appears to be set at the width tabularx has calculated for the first rubber column. MWE:

Code: Select all

\begin{tabularx}{\textwidth}{|l|l|X|}

test1 & test2 & test3 the quick brown fox jumped over the lazy dog a few times to pad out the sentence. \\

test4 & \multicolumn{2}{||X||}{test4 why does this cell content not spread to the width of the entire table? This is strange since the cell border seems to be aligned with the previous one} \\
\end{tabularx}
What am I doing wrong here? Or is there another way to achieve this goal?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx and multicolumn expansion

Post by localghost »

ogtifs wrote:[…] MWE:

Code: Select all

\begin{tabularx}{\textwidth}{|l|l|X|}

test1 & test2 & test3 the quick brown fox jumped over the lazy dog a few times to pad out the sentence. \\

test4 & \multicolumn{2}{||X||}{test4 why does this cell content not spread to the width of the entire table? This is strange since the cell border seems to be aligned with the previous one} \\
\end{tabularx}
What am I doing wrong here? Or is there another way to achieve this goal?
That is not a MWE. I'm not sure what you are aiming at. But if you want a single colum spanning the whole width thus three columns, you have to modify the last table row as shown below.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
%\usepackage[includeheadfoot,margin=2cm]{geometry}
\usepackage{tabularx}
\usepackage{lmodern}

\begin{document}
  \begin{tabularx}{\linewidth}{|l|l|X|}
    test1 & test2 & test3 the quick brown fox jumped over the lazy dog a few times to pad out the sentence. \\
    \multicolumn{3}{||p{\hsize}||}{test4 why does this cell content not spread to the width of the entire table? This is strange since the cell border seems to be aligned with the previous one} \\
  \end{tabularx}
\end{document}
For more information about the \hsize length see the tabularx manual.


Best regards and welcome to the board
Thorsten
ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

tabularx and multicolumn expansion

Post by ogtifs »

I'm sorry if my example was a bit verbose. I don't think I have made my problem clear. Using the multicolumn(with pdflatex from texlive debian unstable) gives me a result something like:

Code: Select all

|---|----|----------|
|asd|asdf|contentcon|
|---|----|----------|
|asd|contentcon     |
|   |tentconten     |
|---|----|----------|
instead of the expected

Code: Select all

|---|----|----------|
|asd|asdf|contentcon|
|---|----|----------|
|asd|contentcontentc|
|   |contentcontentc|
|---|----|----------|
that is to say, the CONTENT of the multicolumn does not expand to fill the table, even though its borders do. The content of the multicolumn appears to be stuck at the width of the 3rd column in the 1st line.
If anyone could explain this behaviour it would be much appreciated
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx and multicolumn expansion

Post by localghost »

ogtifs wrote:[...] If anyone could explain this behaviour it would be much appreciated
Well, if it's only about an explanation, here you are. The tabularx package calculates the width of the »X« type columns from the given width of the table, which is saved in the \hsize length register. Depending on the number of »X« columns, this is a fixed width. Once calculated, it is valid for the entire table. It can be recalculated According to the instructions in the package manual.
ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

Re: tabularx and multicolumn expansion

Post by ogtifs »

I have examined the PDF manual but have found no documented method of recalculating the \hsize parameter.

I expect my best option is to change to using fixed-width p{} columns instead of l{}. This is sub-optimal because I would have to set the first columns overly wide as text wrapping here is undesirable.

Unexpectedly, using \multicolumn{2}{p{\hsize}} on a table only 4cm wide stretches the multicolumn to the width of the page.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx and multicolumn expansion

Post by localghost »

I did a mistake. The length \hsize remains fixed. But it can be used to recalculate the column width of »X« type columns. See Section 4.3 (Column widths, p. 3) of the tabularx manual.
ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

tabularx and multicolumn expansion

Post by ogtifs »

That section describes a procedure for setting the width of an X column as a multiple of the originally calculated \hsize parameter. What I would need here is something like

Code: Select all

new_Xcol_width = <current_Xcol_width> + <spanned_lcol_width>
It seems to me the \multicolumn{2}{X}{} statement should be doing this
ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

tabularx and multicolumn expansion

Post by ogtifs »

If anybody is having this same issue, I have found what is to me a suitable workaround, though it involves heavily restructuring the latex source (hooray for regular expressions). It involves using a nested tabularx with the child's width set to \hsize (the size of the containing X column). In this manner \multicolumn is avoided altogether

Code: Select all

\begin{tabularx}{\textwidth}{l X}
test left side &
   \mbox{\begin{tabularx}{\hsize}{l X}
      this column   & this text will wrap at boundary \\
      will not wrap & blah blah blah \\
   \end{tabularx}} \\
more left side & asdf etc \\
\end{tabularx}
Post Reply