Break multicolumn text at document margin with tabularx

I'm using tabularx to break the content of a column at the document margin (cf. Linebreak in table cell at document margin). But when I use \multicolumn in such a table, the instruction to break the text in that column is ignored. How can I enforce it?

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\newlength{\mylength}
\setlength{\mylength}{\linewidth}

\hrule
\begin{tabularx}{\mylength}{lX}
Lorem ipsum dolor sit amet, consectetuer & adipiscing elit. Ut purus elit, vestibulum ut, placerat ac\\
\multicolumn{2}{l}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac}\\
\end{tabularx}
\hrule

\end{document}


In response to Mico's question:

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\newlength{\mylength}
\setlength{\mylength}{\linewidth}

\hrule
\begin{tabularx}{\mylength}{llX}
Lorem ipsum dolor & sit amet, consectetuer & adipiscing elit. Ut purus elit\\
\end{tabularx}
\hrule
\begin{tabularx}{\mylength}{llX}
Lorem ipsum dolor & sit amet, consectetuer & adipiscing elit. Ut purus elit\\
Lorem ipsum dolor sit & \multicolumn{2}{p{\mylength}}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit}\\
\end{tabularx}
\hrule

\end{document}


-
The issue arises because the column type of the \multicol statement is l, which is not set up to allow line breaks. Use \multicol{2}{p{\mlength}}{...} instead. – Mico Apr 4 '13 at 19:33
@Mico: Assuming you mean "\mylength", that seems reasonable, but then the text is actually broken in (and not at) the margin. The text stretches further than \hrule. And correct me if I'm wrong, but using {p{\mylength}} would also not be applicable in a table where only some of the columns are multicolumns. – Sverre Apr 4 '13 at 19:39
Well, the poor line break happens because LaTeX isn't set up by default to hyphenate a four-letter word (elit,); I trust your actual document has "real" text... Regarding your second comment: Could you post an actual example? I don't see why the p column type couldn't be applied in such a more general setup. – Mico Apr 4 '13 at 20:09
I'll take your word for the elit, thing (although I thought LaTeX would push the word into the next line rather than extend it into the margin?). I've posted an actual example above now. Note that the second table also messes up the line breaking in the first row, for some reason ... – Sverre Apr 4 '13 at 20:19

The following is a version of your second MWE. Note that because the width of the entire tabularx environment is given by \mylength, you mustn't use that parameter for the combined width of the second and third columns. To compute the correct width, I make use of two extra length variables, \xlength and \ylength. \xlength is set to the width of the first column (+4\tabcolsep); then, \ylength is computed as \mylength-\xlength.

\documentclass{article}
\usepackage{tabularx}
\hyphenation{ad-ip-is-cing} % no idea if this is correct...

\newlength\xlength
\settowidth\xlength{Lorem ipsum dolor sit}

\newlength\mylength
\setlength\mylength{\linewidth}

\newlength\ylength
\setlength\ylength\mylength

\begin{document}
\hrule
\begin{tabularx}{\mylength}{llX}
Lorem ipsum dolor & sit amet, consectetuer &
Lorem ipsum dolor sit &
\multicolumn{2}{p{\ylength}}{Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Ut purus elit}\\
\end{tabularx}
\hrule
\end{document}


• The calculation of \xlength along the lines given in the code above is needed because the first column, which is of type l, doesn't have a pre-specified width. If you're willing to change its column type to p, the width of that column could be used directly to calculate the width of the multicolumn.

E.g., if the width of the first column is given by \xlength (up to you to choose its value...), you might start the tabularx environment as follows:

\begin{tabularx}{\mylength}{p{\xlength}lX}


Note that \xlength now is purely the width of the first column, i.e., it does not include the term 4\tabcolsep.

Then, later on in this tabularx environment, you could specify the width of the two-column \multicolumn (of type p, as in the earlier example) as follows:

\multicolumn{2}{p{\dimexpr\mylength-\xlength-4\tabcolsep}}{<stuff>}

• The term 4\tabcolsep is the sum of

• 1\tabcolsep at the left-hand edge of the table,
• 2\tabcolsep for the amount of whitespace between columns 1 and 2, and
• 1\tabcolsep at the right-hand edge of the table.
-
+1 Next time I'll fix things so that you can use X to span any number of columns and it will do this calculation for you. Next time..... – David Carlisle Apr 4 '13 at 21:42
One issue here is that you need to find out post-hoc what the length of the non-multicolumns are by correctly locating the cells with the widest text widths. Ideally this should somehow be done automatically, which I think (hope!) is what David says he will do in the next version (!) of tabularx. Could you briefly explain the 4\tabcolsep thing? Why exactly that? – Sverre Apr 5 '13 at 13:09
@Sverre - I've provided an addendum to address your follow-up questions. – Mico Apr 5 '13 at 15:00
@Mico: Nice addendum. Your more detailed explanation will be valuable also to others who google things like 4\tabcolsep (like I tried first). – Sverre Apr 6 '13 at 15:30