TransWikia.com

How to use siunitx and tabularx together?

TeX - LaTeX Asked by janitor048 on August 12, 2021

I’m using the tabular features of siunitx (for what it is worth: v1.3 since I’m on TexLive 2009) to align numbers in tables, eg.

begin{tabular}{ p{1.5cm} S S S }
bla & 1.23 & 4.5 & 67.89 tabularnewline
end{tabular}

and that works perfectly fine.

Now I would like to have this table span a specific width, or rather to have several tables to have the same width. For this I would normally use tabularx

begin{tabularx}{textwidth}{ p{1.5cm} X X X }
bla & 1.23 & 4.5 & 67.89 tabularnewline
end{tabular}

Now, is there a way to combine the two? To have the number formatting and alignment features of siunitx inside an automatically resizing table?

4 Answers

The X column in tabularx is then converted to p{<width>} where width is automatically calculated. You can change this by redefining the tabularxcolumn macro like described in the package manual:

renewcommand{tabularxcolumn}[1]{<column definition where #1 is the width>}

The S column uses the c column internally. To replace this with p{<width>} you have to manually place the internal column definition of S into tabularxcolumn. The following code worked in my tests.

documentclass{article}

usepackage{array}
usepackage{tabularx}
usepackage{siunitx}


begingroup
% Allow `_` and `:` in macro names (LaTeX3 style)
catcode`_=11
catcode`:=11
% Internal code of `S`
gdeftabularxcolumn#1{%
    >{__siunitx_table_collect_begin:Nn S{} }%
    p{#1}%  <- this is different (is `c` in normal `S`)
    <{__siunitx_table_print:}%
}
endgroup

begin{document}

begin{tabularx}{textwidth}{p{1.5cm} XXX}
     bla & 1.23 & 4.5  & 67.89 
     bla & 1.2  & 4.50 &  7.89 
     bla &  .2  &  .50 & 67.8 
end{tabularx}
end{document}

Correct answer by Martin Scharrer on August 12, 2021

Taking Martin's solution and re-coding for version 1 of siunitx, you'd do something like

documentclass{article}

usepackage{siunitx,tabularx}

makeatletter
deftabularxcolumn#1{%
    >{si@tab@begin@S[]}%
    p{#1}%  <- this is different (is `c` in normal `S`)
    <{si@tab@end@S}%
}
makeatother

begin{document}

begin{tabularx}{textwidth}{p{1.5cm} XXX}
     bla & 1.23 & 4.5  & 67.89 
     bla & 1.2  & 4.50 &  7.89 
     bla &  .2  &  .50 & 67.8 
end{tabularx}
end{document}

Answered by Joseph Wright on August 12, 2021

The quite new tabu package seems to be very encouraging. Section 1.5 of its manual introduces explicitly an example for how to embed siunitx's »S« columns in »X« columns. Regarding tables this package seems to be an allrounder.

Answered by Thorsten Donig on August 12, 2021

Here's a variant of @DavidCarlisle's answer for siunitx version 3:

documentclass{article}
% usepackage{array}
usepackage{tabularx}
usepackage{siunitx}

begingroup
% Allow `_` and `:` in macro names (LaTeX3 style)
catcode`_=11
catcode`:=11
gdeftabularxcolumn#1{%
    >{mode_leave_vertical: siunitx_cell_begin:w centering}%
    p{#1}%
    <{siunitx_cell_end:}%
}
endgroup

begin{document}
noindent
begin{tabularx}{textwidth}{p{1.5cm}XXX}
     bla & 1.23 & 4.5  & 67.89 
     bla & 1.2  & 4.50 &  7.89 
     bla &  .2  &  .50 & 67.8 
end{tabularx}
end{document}

Also check https://github.com/josephwright/siunitx/issues/423 for improved versions - I removed ignorespaces to make it compile with color{red} 4.50, but alignment of colored cells is still off.

Answered by bers on August 12, 2021

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