TransWikia.com

Full-width tabular with column width as needed

TeX - LaTeX Asked by olidem on September 12, 2020

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 .47textwidth each) inside.

begin{tabular}
begin{minipage}[t]{0.47columnwidth}
left outer
end{minipage} &
begin{minipage}[t]{0.47columnwidth}
    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.

2 Answers

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.

Answered by Michael Bölting on September 12, 2020

To obtain X columns with lengths in a certain ratio, you have to use the syntax >hsize=x_ihsize}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.6hsize}X>{hsize=0.4hsize}X.

Answered by Bernard on September 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP