LaTeX users online

In total there are 7 users online :: 0 registered, 0 hidden and 7 guests (based on users active over the past 5 minutes)
Most users ever online was 1327 on Tue Nov 05, 2013 7:11 pm

Users browsing this forum: No registered users and 7 guests

LaTeX Friends

Sponsored by DANTE e.V.: The German speaking TeX Users Group

tabularx and multicolumn expansion

LaTeX Forum: Graphics, Figures & Tables

Add tags Information and discussion about graphics, figures & tables in LaTeX documents.
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  •  Open in online LaTeX editor
\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?
ogtifs

Posts: 8
Joined: Mon Dec 28th, 2009

ogtifs wrote:[…] MWE:
Code: Select all  •  Open in online LaTeX editor
\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  •  Open in online LaTeX editor
\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}

Best regards and welcome to the board
Thorsten
LaTeX Community Moderator

¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

localghost
Site Moderator

Posts: 9218
Joined: Fri Feb 2nd, 2007
Location: Braunschweig, Germany

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  •  Open in online LaTeX editor
|---|----|----------||asd|asdf|contentcon||---|----|----------||asd|contentcon     ||   |tentconten     ||---|----|----------|

Code: Select all  •  Open in online LaTeX editor
|---|----|----------||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
ogtifs

Posts: 8
Joined: Mon Dec 28th, 2009

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.
LaTeX Community Moderator

¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

localghost
Site Moderator

Posts: 9218
Joined: Fri Feb 2nd, 2007
Location: Braunschweig, Germany

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.
ogtifs

Posts: 8
Joined: Mon Dec 28th, 2009

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.
LaTeX Community Moderator

¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

localghost
Site Moderator

Posts: 9218
Joined: Fri Feb 2nd, 2007
Location: Braunschweig, Germany

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  •  Open in online LaTeX editor
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 28th, 2009

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  •  Open in online LaTeX editor
\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}
ogtifs

Posts: 8
Joined: Mon Dec 28th, 2009