[Tex/LaTex] multicolumn X paragraph

longtablelongtabumulticolumnparagraphstables

\documentclass[a4paper,10pt]{article}
\usepackage{array,tabularx,longtable,tabu}
\usepackage{lipsum}

\begin{document}

\begin{longtabu} to \linewidth {Xccr}
    \lipsum[1] & Val1/1 & Val1/2 & Qty 1 \\
    \multicolumn{2}{l}{\lipsum[2]} & Qty 2 \\
\end{longtabu}

\end{document}

Looks like this:

enter image description here

Strange behavior: if I don't use multicolumn then a simple X column becomes a paragraph automatically. (E.g. instead of X[p] I can use X, not sure why.)

However, if I use a multicolumn, then it won't be a paragraph automatically.

Okay, so I could do this:

\begin{longtabu} to \linewidth {Xccr}
    \lipsum[1] & Val1/1 & Val1/2 & Qty 1 \\
    \multicolumn{3}{p{5cm}}{\lipsum[2]} & Qty 2 \\
\end{longtabu}

But here I had to specify a fixed width of 5cm and it looks bad:

enter image description here

I do not want fixed with. I want to use available space. In this case: all space available in the first three columns. I can do that by using a simple \multicolumn{3}{l} but then it won't be a paragraph. Or I can do \multicolumn{3}{p{5cm}} and it will be a paragraph but then it won't have flexible size.

Why can't I do both at the same time? E.g. have a paragraph with flexible width?

The problem is independent of the size of the page or the width of the table itself, so the solution MUST also be independent. If course I could measure the required widths with a ruler and write in fixed values, but this is not what I want. The solution should work when I change the paper size, the orientation, or the width of the table etc. I have tried these, and none of them is going to work:

\multicolumn{3}{l}{\lipsum[2]} % not a pararaph
\multicolumn{3}{p{5cm}}{\lipsum[2]} % not flexible width
\multicolumn{3}{p{X}}{\lipsum[2]} % X cannot be used here
\multicolumn{3}{p}{\lipsum[2]} % missing argument for width
\multicolumn{3}{X[p,l]}{\lipsum[2]} % X cannot be used here (why?)

I do not seem to find a solution even for \multicolumn{1}, so there must be something fundamental here that I don't see. There are a group of questions about multi columns + auto colum sizing that are always returning and I can never find a good solution. It seems that there is something about multi columns + column sizings that just doesn't work right, or at least nobody knows how to do it the way I have imagined. I have asked a more general question in this topic before ( https://tex.stackexchange.com/questions/202103/ ) that would solve all of my problems, put a bounty on it and did not get an answer at all. This particular question is a specialized version of the general case, because in this special case there is only one X column and all column widths could be calculated by measuring the space required for the last three columns. The size required for the last three columns can be calculated easily, it is very straightforward! But for some reason, I cannot find a good solution even in this very simple case.

I'm a programmer. How hard would it be to develop my own column size calculations the way I like them? I'm not very good at C programming. Do I have to do this in C, or would it be possible to do it in plain TeX? LateX seems to have a long learning curve and the language itself has a very cryptic syntax, but if it would be possible, then maybe I'll start working on it. I guess it would require deep knowledge about TeX internals which I don't possess yet.

Best Answer

You can retrieve the column sizes from the aux file where longtable stores them: You will need to delete the .aux initially to avoid picking up bad lengths from earlier attempts.

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage{array,tabularx,longtable,tabu}
\usepackage{lipsum}

\begin{document}

\makeatletter
\ifx\LT@i\@undefined
\def\mcspec#1{}
\else
 \global\dimen1=\z@
{\count@=\z@
 \def\LT@entry#1#2{%
 \advance\count@\@ne
 \ifnum\count@<4 \global\advance\dimen1 #2\relax\fi}
 \LT@i}
  \advance\dimen1 -2\tabcolsep
  \edef\mcspec{\noexpand\multicolumn{3}{p{\the\dimen1}}}


\fi
%\show\mcspec
\makeatother




\begin{longtabu} to \linewidth {Xccr}
    \lipsum[1] & Val1/1 & Val1/2 & Qty 1 \\
    \mcspec{\lipsum[2]} & Qty 2 \\
\end{longtabu}

\end{document}
Related Question