LaTeX forum ⇒ Graphics, Figures & Tablestabularx and multicolumn expansion

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

tabularx and multicolumn expansion

Postby ogtifs » Mon Dec 28, 2009 3:11 am

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:
  1. \begin{tabularx}{\textwidth}{|l|l|X|}
  2.  
  3. test1 & test2 & test3 the quick brown fox jumped over the lazy dog a few times to pad out the sentence. \\
  4.  
  5. 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} \\
  6. \end{tabularx}


What am I doing wrong here? Or is there another way to achieve this goal?

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Sat Jan 02, 2010 1:28 pm

ogtifs wrote:[…] MWE:
  1. \begin{tabularx}{\textwidth}{|l|l|X|}
  2.  
  3. test1 & test2 & test3 the quick brown fox jumped over the lazy dog a few times to pad out the sentence. \\
  4.  
  5. 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} \\
  6. \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.
  1. \documentclass[11pt,a4paper,english]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{babel}
  5. %\usepackage[includeheadfoot,margin=2cm]{geometry}
  6. \usepackage{tabularx}
  7. \usepackage{lmodern}
  8.  
  9. \begin{document}
  10. \begin{tabularx}{\linewidth}{|l|l|X|}
  11. test1 & test2 & test3 the quick brown fox jumped over the lazy dog a few times to pad out the sentence. \\
  12. \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} \\
  13. \end{tabularx}
  14. \end{document}

For more information about the \hsize length see the tabularx manual.


Best regards and welcome to the board
Thorsten
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


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

Link:
BBcode:
HTML:
Hide post links
Show post links

ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

Postby ogtifs » Sat Jan 09, 2010 10:46 am

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:
  1. |---|----|----------|
  2. |asd|asdf|contentcon|
  3. |---|----|----------|
  4. |asd|contentcon |
  5. | |tentconten |
  6. |---|----|----------|

instead of the expected
  1. |---|----|----------|
  2. |asd|asdf|contentcon|
  3. |---|----|----------|
  4. |asd|contentcontentc|
  5. | |contentcontentc|
  6. |---|----|----------|

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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Sat Jan 09, 2010 3:59 pm

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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


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

Link:
BBcode:
HTML:
Hide post links
Show post links

ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

Postby ogtifs » Sun Jan 10, 2010 11:44 pm

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.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Mon Jan 11, 2010 12:23 am

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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


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

Link:
BBcode:
HTML:
Hide post links
Show post links

ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

Postby ogtifs » Thu Jan 14, 2010 1:13 am

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
  1. new_Xcol_width = <current_Xcol_width> + <spanned_lcol_width>

It seems to me the \multicolumn{2}{X}{} statement should be doing this

Link:
BBcode:
HTML:
Hide post links
Show post links

ogtifs
Posts: 8
Joined: Mon Dec 28, 2009 3:08 am

Postby ogtifs » Fri Jan 29, 2010 10:36 pm

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

  1. \begin{tabularx}{\textwidth}{l X}
  2. test left side &
  3. \mbox{\begin{tabularx}{\hsize}{l X}
  4. this column & this text will wrap at boundary \\
  5. will not wrap & blah blah blah \\
  6. \end{tabularx}} \\
  7. more left side & asdf etc \\
  8. \end{tabularx}

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 3 guests