1
\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.

4

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}
  • I don't understand the half of it, but it works. :-) Would it be possible to make it a column type instead of a new command? Or do I have to create a custom command for each and every case? I'll try to understand how it works. – nagylzs Oct 23 '14 at 12:09
  • @nagylzs well some user interface command could be defined to hide some of it but you need to know the length before the table starts, so it can lay out the table and obviously the columns to add up LT@i is the data for the first longtable (roman numerals) and the \ifnum\count@<4 says to add up the first 3 columns, so they would need changing each time – David Carlisle Oct 23 '14 at 12:15
  • I would like to know more about this. There has to be a way to make it general, just I don't know enough about TeX to do it. So I won't write more comments until I understand how this works. But where should I start? Should I start reading documentation about the TeX internals that you have used? Does it require special knowledge about how a longtable stores data in the aux file? Is that documented somewhere? – nagylzs Oct 23 '14 at 12:22
  • I could modify this code to use a different number of columns. But how do I skip columns? How can I tell that \dimen1 should be the size of columns between 2 and 4? Is there a way to generalize this so that instead of using LT@i I can reference to a table? This macro in its current form cannot be used for large documents with many tables, nor can it be used when tables are included from different TeX files... – nagylzs Oct 24 '14 at 7:34
  • I could skip columns: \ifnum1<\count@<4 but LI@i is still a problem. Is there a way to pass the number of the "current longtable" from a macro call? – nagylzs Oct 24 '14 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.