0

I am translating HTML-tables to latex counterparts.

In HTML, I have a table with full width that distributes the column according to their content.

So this leads to equally spaced columns,

<table style="width: 100%;">
    <tr>
        <td>a</td>
        <td>a</td>
    </tr>
</table>

whereas this assigns 4 times as much space to the left column:

<table style="width: 100%;">
    <tr>
        <td>aaaa</td>
        <td>a</td>
    </tr>
</table>

See also here for a nested example: https://jsfiddle.net/49nhmqLu/1/

tabularx

I have found that for tabularx an X column takes the remaining space:

\begin{tabularx}{\textwidth}{lX}

but there multiple X-columns would distribute the width equally, not as needed by the content. (XX would be 50:50, XXX would be 33:33:33)

My question: Is it possible to have a column type that distributes width relatively to the content? like my aaaa - a (i.e. 80:20 here) example?

Additional requirement

Actually, the content of the tabular cells may itself consist of tabularx's 🙈. So I have found that tabularx-in-tabularx seems to be impossible.

If I use outer tabulars, i can only arrive at full textwidth by having minipages inside:

If I do not insist on the content-aware column widths, I can use tabular outside and use minipages with fixed widths (eg two columns with .47\textwidth each) inside.

\begin{tabular}
\begin{minipage}[t]{0.47\columnwidth}
left outer
\end{minipage} &
\begin{minipage}[t]{0.47\columnwidth}
    \begin{tabularx}{\textwidth}{lX}
    inner left &
    inner right
    \end{tabularx}
\end{minipage}
\\
\end{tabular}

I am absolutely clueless how to get an automatic width distribution to work here, the latter example will always be 50:50.

Note that I am not insiting on an outer tabular. Anything else that distributes with according to content is fine as well. In HTML one could use e.g. <div>'s here

In the best case there would exist an environment tabularxyz which allows:

\begin{tabularxyz}
  left outer
&
  \begin{tabularx}{\textwidth}{lX}
    inner left &
    inner right
  \end{tabularx}
\\
\end{tabularxyz}

where Latex determines the optimal width for all columns, just as tabulary does this (but only for plain text contents).

The nested tabularx's are the result of a placeholder substitution from my reporting software. Most of the time, they are even just used without a surrounding table. I use tabularx because it allows to use full available textwidth.

9
  • Hello and welcome. I didn't understand what the problem is that you're trying to solve. – AndréC Jul 16 '20 at 5:46
  • I improved my question – olidem Jul 16 '20 at 5:55
  • Unless I'm mistaken, it seems to me that what you want is the default behavior of the tabular environment. – AndréC Jul 16 '20 at 6:11
  • 1
    You could take a look at the tabulary package. – leandriis Jul 16 '20 at 6:48
  • 1
    Regarding "tabularx-in-tabularx seems to be impossible": How to put tabularx inside tabularx might be interesting. Unfortunately this does not seem to be adaptable to tabulary. – leandriis Jul 16 '20 at 7:02
0

tabular* will fix the tabular's width to the textwidth.

For the tabular inside a tabular cell, you could use the multirow package. You can specify cells containing multiple rows/columns (and both) with it. Examples can be found here.

0

To obtain X columns with lengths in a certain ratio, you have to use the syntax >\hsize=x_i\hsize}X, where x_i is a coefficient for the i-th X column which is proportional to the desired fraction of the total free length, and such that the sum of the coefficients is equal to the total number of X columns.

For example, to have the ratio 80/20 with two X columns, you would have to solve the (easy) linear system:

x_1/80=x_2/40, x_1+x_2=2, whence, in the table preamble:

>{\hsize=1.6\hsize}X>{\hsize=0.4\hsize}X.

2
  • Thanks, if I knew the desired width ratio in advance, this would be the way to go. – olidem Jul 16 '20 at 9:49
  • If you don't know in advance, I'm afraid it would be by trial and error. – Bernard Jul 16 '20 at 9:54

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.