2

I have a table that is narrower than a page, and would like to pad each column by an equal amount to fill the available width.

(The aim is to create more space around the narrower columns; as I am generating tables programmatically, adding a fixed amount of space risks unintentionally making the table wider than the available space.)

There are some solutions proposed online that set equal column widths; this will not work for me, as one column is wider than the rest, and so that column ends up wrapping if equal column widths are imposed.

If I have a tabular of the form:

\begin{tabular}{|c|c|c|c|c|} Somewhat longer column heading & $x$ & $y$ & $z$ \\ \hline A long but one-line description & 3.1 & 4.1 & 5.9 \\ Another long-ish description that shouldn't crash & 2.7 & 1.8 & 2.8 \end{tabular} 

Then what I'm looking for is something like:

example table image

where I've marked the page margins in red. (Number alignment is slightly off because I faked this by adding a bunch of ~s.)

(The numbers are simplified; in reality they will be a mix of integers and numbers with error bars, so I can't use d columns to align on the decimal point.)

Thing that don't work

tabular*

Trying this solution comes close, but no space is added at the left, and inter-column lines are misplaced.

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}|c|c|c|c|c|} 

tabular* illustration

tabularx

tabularx appears to only be able to set even widths, not spacings, so the left column wraps and the right columns get too much space:

Preamble:

\newcolumntype{Y}{>{\centering\arraybackslash}X} 

Table header:

\begin{tabularx}{\textwidth}{|Y|Y|Y|Y|Y|} 

tabularx illustration

tabulary

tabulary seems to ignore the {\textwidth} option; I'm not sure if this is a bug.

\begin{tabulary}{\textwidth}{|C|C|C|C|C|} 

tabulary illustration

tabularray

LaTeX3's tabularray/tblr comes close setting the relative column widths to -1, but it seems to act multiplicatively rather than additively, so the wide column gets too much space.

\begin{tblr}{width=\textwidth,colspec={|X[-1,c]|X[-1,c]|X[-1,c]|X[-1,c]|}} 

tblr illustration, -1

(Using relative widths of 0 or 1 gives the same problems as tabulary and tabularx above respectively.)

Is there a way to get equal space added per column while still placing the inter-column lines correctly?

9
  • 2
    tabular* doesn't add space on the left as you used @ you could use ! from array package. (but the | are still misplaced unless you put them in their own column) tabularx can adjust the width of the first column see the \hsize=1.5\hsize examples in the manual or this site. tabulary not over-expanding is a documented feature (there is some code on this site to over-ride that) because 999 times out of 1000 stretching a table to full width just makes it harder to read for no benefit so in my innocent youth I thought no one would ever want to do that. Commented Nov 28, 2024 at 15:49
  • tabulary tex.stackexchange.com/a/87543/1090 Commented Nov 28, 2024 at 15:52
  • Thanks for the link to the older tabulary thread—using that incantation however gives an Arithmetic overflow. Commented Nov 28, 2024 at 16:05
  • on the example there or the one here? tex dimen arithmetic is weird, avoiding overflow is a black art I may look later Commented Nov 28, 2024 at 16:06
  • 1
    @Mico Well yes, that's what I'm looking for a way of avoiding doing… Commented Nov 28, 2024 at 23:33

2 Answers 2

4

tabularx and tabulary adjust the table width by affecting the target column width for line breaking so shouldn't be applied to numeric data columns. All the numeric columns will naturally have the same width (and align on the decimal point) if you specify a suitable column type, then you just need one X column to fill out the rest of the space.

enter image description here

\documentclass{article} \usepackage{dcolumn,tabularx} \newcolumntype{d}{D{.}{.}{4.2}} \begin{document} \noindent X\dotfill X \begin{center} \begin{tabularx}{\textwidth}{|X|d|d|d|d|} Somewhat longer column heading & $x$ & $y$ & $z$ \\ \hline A long but one-line description & 333.1 & 4.1 & 5.9 \\ Another long-ish description that shouldn't crash & 2.7 & 1.8 & 22.8 \end{tabularx} \end{center} \noindent X\dotfill X \end{document} 
3
  • Thanks for the reply—I oversimplified my example; in my actual use case the narrower columns will contain a mix of numbers with no decimal points and numbers with error bars, neither of which is expected to align nicely on a decimal point. (I'm also confident that the numeric columns should never wrap.) The main reason I'm trying to widen the table is to give the numbers more breathing space, so making the text column expand doesn't achieve what I'm looking for. Commented Nov 28, 2024 at 16:07
  • both dcolumn and siunitx (the other package for decimal alignment) have mechanisms fot avoiding alignment in specific cells (siunitix is better at that) or you could just use wc{4em} or wr{4em} or whatever width you need for forced width centred or right aligned columns. @EdBennett Commented Nov 28, 2024 at 16:10
  • 1
    more breathing space is better done by changing the format eg change 4.2 to 5.3 to make every column 2 digits wider or \addtolength\tabcolsep{5pt} to add 5pt more space on each side of every column. Also of course removing the vertical rules always improves a table (see booktabs package documentation on the use of vertical rules) @EdBennett Commented Nov 28, 2024 at 16:12
3

With the following code you can specify the proportion of the width allotted for the first column and the total number of columns; the ones after the first will have equal width.

\documentclass{article} \usepackage[a4paper,margin=2cm]{geometry} \usepackage{tabularx} \newcolumntype{Y}[1]{>{\centering\arraybackslash\hsize=#1\hsize}X} \ExplSyntaxOn \NewDocumentEnvironment{bennettx}{mmm+b} {% #1 = width, #2 = number of columns, #3 = proportion for the first column \fp_set:Nn \l_tmpa_fp { #3*#2 } \fp_set:Nn \l_tmpb_fp { #2*(1-#3)/(#2-1) } % prepare the preamble \tl_set:Ne \l_tmpa_tl { | Y{\fp_use:N \l_tmpa_fp} | *{\int_eval:n {#2-1}}{Y{\fp_use:N \l_tmpb_fp}|} } \exp_args:NnnV \begin{tabularx}{#1} \l_tmpa_tl #4 \end{tabularx} }{} \ExplSyntaxOff \begin{document} \noindent \begin{bennettx}{\textwidth}{4}{0.6} \hline Somewhat longer column heading & $x$ & $y$ & $z$ \\ \hline A long but one-line description & 3.1 & 4.1 & 5.9 \\ Another long-ish description that shouldn't crash & 2.7 & 1.8 & 2.8 \\ \hline \end{bennettx} \bigskip \noindent \begin{bennettx}{\textwidth}{4}{0.7} \hline Somewhat longer column heading & $x$ & $y$ & $z$ \\ \hline A long but one-line description & 3.1 & 4.1 & 5.9 \\ Another long-ish description that shouldn't crash & 2.7 & 1.8 & 2.8 \\ \hline \end{bennettx} \bigskip \noindent \begin{bennettx}{0.6\textwidth}{4}{0.7} \hline Somewhat longer column heading & $x$ & $y$ & $z$ \\ \hline A long but one-line description & 3.1 & 4.1 & 5.9 \\ Another long-ish description that shouldn't crash & 2.7 & 1.8 & 2.8 \\ \hline \end{bennettx} \end{document} 

output

3
  • Thanks for the reply—I think I could achieve a similar thing by adjusting the weights in the tblr example; I was hoping for a solution where I didn't need to know the relative column widths a priori. Commented Nov 28, 2024 at 23:34
  • @EdBennett If only you don't insist in ruled (caged) tables… Commented Nov 28, 2024 at 23:56
  • Both the spacing and the cages are at my collaborators' insistence, unfortunately. Commented Nov 29, 2024 at 0:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.