Featured Book

LaTeX Beginners Guide

Who is online

In total there are 15 users online :: 1 registered, 0 hidden and 14 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: Yahoo [Bot] and 14 guests

Partner Sites

TeXwelt - Fragen und Antworten goLaTeX
TeXblog
TeXnicCenter
TeXample

tabularx and multicolumn expansion

Add tags Information and discussion about graphics, figures & tables in LaTeX documents.

tabularx and multicolumn expansion

Postby ogtifs on Mon Dec 28th, 2009

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 writeLaTeX
\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

Re: tabularx and multicolumn expansion

Postby localghost on Sat Jan 2nd, 2010

ogtifs wrote:[…] MWE:
Code: Select all  •  Open in writeLaTeX
\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 writeLaTeX
\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
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
User avatar
localghost
Site Moderator
 
Posts: 9219
Joined: Fri Feb 2nd, 2007
Location: Braunschweig, Germany

Re: tabularx and multicolumn expansion

Postby ogtifs on Sat Jan 9th, 2010

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 writeLaTeX
|---|----|----------|
|asd|asdf|contentcon|
|---|----|----------|
|asd|contentcon     |
|   |tentconten     |
|---|----|----------|

instead of the expected
Code: Select all  •  Open in writeLaTeX
|---|----|----------|
|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

Re: tabularx and multicolumn expansion

Postby localghost on Sat Jan 9th, 2010

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
User avatar
localghost
Site Moderator
 
Posts: 9219
Joined: Fri Feb 2nd, 2007
Location: Braunschweig, Germany

Re: tabularx and multicolumn expansion

Postby ogtifs on Sun Jan 10th, 2010

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

Re: tabularx and multicolumn expansion

Postby localghost on Mon Jan 11th, 2010

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
User avatar
localghost
Site Moderator
 
Posts: 9219
Joined: Fri Feb 2nd, 2007
Location: Braunschweig, Germany

Re: tabularx and multicolumn expansion

Postby ogtifs on Thu Jan 14th, 2010

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 writeLaTeX
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

Re: tabularx and multicolumn expansion

Postby ogtifs on Fri Jan 29th, 2010

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 writeLaTeX
\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


Return to Graphics, Figures & Tables

Who is online

Users browsing this forum: Yahoo [Bot] and 14 guests